当前位置: 代码迷 >> Android >> Android学习札记之百度地图(根据地名查询经纬度)
  详细解决方案

Android学习札记之百度地图(根据地名查询经纬度)

热度:117   发布时间:2016-05-01 20:38:26.0
Android学习笔记之百度地图(根据地名查询经纬度)

Android?通过地名获得经纬度并标识在地图上???--?未审核

编辑文档

要是调用GeocodergetFromLocationName(),该方法可以传入地名。

  在使用该方法前需要geo?=?new?Geocoder(this,?Locale.CHINA);

  不然在地图上是查询不到的。

  Java代码

  /**

  *

  */

  package?com.decarta.demo;

  import?java.io.IOException;

  import?java.util.List;

  import?java.util.Locale;

  import?Android.app.AlertDialog;

  import?android.app.Dialog;

  import?android.content.Context;

  import?android.graphics.Bitmap;

  import?android.graphics.BitmapFactory;

  import?android.graphics.Canvas;

  import?android.graphics.Point;

  import?android.location.Address;

  import?android.location.Geocoder;

  import?android.os.Bundle;

  import?com.google.android.maps.GeoPoint;

  import?com.google.android.maps.MapActivity;

  import?com.google.android.maps.MapController;

  import?com.google.android.maps.MapView;

  import?com.google.android.maps.Overlay;

  import?com.google.android.maps.Projection;

  /**

  [email protected]

  *

  */

  public?class?Main?extends?MapActivity?{

  //?地图显示控制相关变量定义

  private?MapView?map?=?null;

  private?MapController?mapCon;

  private?Geocoder?geo;

  private?static?final?int?ERROR_DIALOG?=?1;

  /**?Called?when?the?activity?is?first?created.?*/

  @Override

  public?void?onCreate(Bundle?savedInstanceState)?{

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  geo?=?new?Geocoder(this,?Locale.CHINA);

  //?获取MapView

  map?=?(MapView)?findViewById(R.id.map);

  //?设置显示模式

  map.setTraffic(true);

  map.setSatellite(false);

  map.setStreetView(true);

//?设置可以缩放

  map.setBuiltInZoomControls(true);

  List

addresses?=?null;
  try?{

  addresses?=?geo.getFromLocationName("江苏省苏州市寒山寺",?1);

  }?catch?(IOException?e)?{

  //?TODO?Auto-generated?catch?block

  e.printStackTrace();

  }

  if(addresses.size()?==?0)?{

  showDialog(ERROR_DIALOG);

  GeoPoint?geoBeijing?=?new?GeoPoint(

  (int)?(39.906033*?1000000),

  (int)?(116.397700?*?1000000));

  mapCon?=?map.getController();

  mapCon.setCenter(geoBeijing);

  mapCon.setZoom(4);

  }?else?{

  Address?address?=?addresses.get(0);

  //?设置初始地图的中心位置

  GeoPoint?geoPoint?=?new?GeoPoint(

  (int)?(address.getLatitude()?*?1000000),

  (int)?(address.getLongitude()?*?1000000));

  mapCon?=?map.getController();

  mapCon.setCenter(geoPoint);

  mapCon.setZoom(16);

  List?overlays?=?this.map.getOverlays();

  overlays.add(new?PositionOverlay(geoPoint,?this,?R.drawable.ic_red_pin));

  }

  }

  @Override

  protected?boolean?isRouteDisplayed()?{

  return?false;

  }

  @Override

  protected?Dialog?onCreateDialog(int?id)?{

  return?new?AlertDialog.Builder(this).setTitle("查询出错哦")

  .setMessage("路名/地名出错,请重新输入!").create();

  }

  class?PositionOverlay?extends?Overlay?{

  private?GeoPoint?geoPoint;

  private?Context?context;

  private?int?drawable;

  public?PositionOverlay(GeoPoint?geoPoint,?Context?context,?int?drawable)?{

  super();

  this.geoPoint?=?geoPoint;

  this.context?=?context;

  this.drawable?=?drawable;

  }

  @Override

  public?void?draw(Canvas?canvas,?MapView?mapView,?boolean?shadow)?{

Projection?projection?=?mapView.getProjection();

  Point?point?=?new?Point();

  projection.toPixels(geoPoint,?point);

  Bitmap?bitmap?=?BitmapFactory.decodeResource(context.getResources(),

  drawable);

  canvas.drawBitmap(bitmap,?point.x-bitmap.getWidth()/2?,?point.y-bitmap.getHeight(),?null);

  super.draw(canvas,?mapView,?shadow);

  }

  }

  }

  /**

  *

  */

  package?com.decarta.demo;

  import?java.io.IOException;

  import?java.util.List;

  import?java.util.Locale;

  import?android.app.AlertDialog;

  import?android.app.Dialog;

  import?android.content.Context;

  import?android.graphics.Bitmap;

  import?android.graphics.BitmapFactory;

  import?android.graphics.Canvas;

  import?android.graphics.Point;

  import?android.location.Address;

  import?android.location.Geocoder;

  import?android.os.Bundle;

  import?com.google.android.maps.GeoPoint;

  import?com.google.android.maps.MapActivity;

  import?com.google.android.maps.MapController;

  import?com.google.android.maps.MapView;

  import?com.google.android.maps.Overlay;

  import?com.google.android.maps.Projection;

  /**

  [email protected]

  *

  */

  public?class?Main?extends?MapActivity?{

  //?地图显示控制相关变量定义

  private?MapView?map?=?null;

  private?MapController?mapCon;

  private?Geocoder?geo;

  private?static?final?int?ERROR_DIALOG?=?1;

  /**?Called?when?the?activity?is?first?created.?*/

  @Override

  public?void?onCreate(Bundle?savedInstanceState)?{

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  geo?=?new?Geocoder(this,?Locale.CHINA);

//?获取MapView

  map?=?(MapView)?findViewById(R.id.map);

  //?设置显示模式

  map.setTraffic(true);

  map.setSatellite(false);

  map.setStreetView(true);

  //?设置可以缩放

  map.setBuiltInZoomControls(true);

  List

addresses?=?null;
  try?{

  addresses?=?geo.getFromLocationName("江苏省苏州市寒山寺",?1);

  }?catch?(IOException?e)?{

  //?TODO?Auto-generated?catch?block

  e.printStackTrace();

  }

  if(addresses.size()?==?0)?{

  showDialog(ERROR_DIALOG);

  GeoPoint?geoBeijing?=?new?GeoPoint(

  (int)?(39.906033*?1000000),

  (int)?(116.397700?*?1000000));

  mapCon?=?map.getController();

  mapCon.setCenter(geoBeijing);

  mapCon.setZoom(4);

  }?else?{

  Address?address?=?addresses.get(0);

  //?设置初始地图的中心位置

  GeoPoint?geoPoint?=?new?GeoPoint(

  (int)?(address.getLatitude()?*?1000000),

  (int)?(address.getLongitude()?*?1000000));

  mapCon?=?map.getController();

  mapCon.setCenter(geoPoint);

  mapCon.setZoom(16);

  List?overlays?=?this.map.getOverlays();

  overlays.add(new?PositionOverlay(geoPoint,?this,?R.drawable.ic_red_pin));

  }

  }

  @Override

  protected?boolean?isRouteDisplayed()?{

  return?false;

  }

  @Override

  protected?Dialog?onCreateDialog(int?id)?{

  return?new?AlertDialog.Builder(this).setTitle("查询出错哦")

  .setMessage("路名/地名出错,请重新输入!").create();

  }

  class?PositionOverlay?extends?Overlay?{

  private?GeoPoint?geoPoint;

  private?Context?context;

  private?int?drawable;

  public?PositionOverlay(GeoPoint?geoPoint,?Context?context,?int?drawable)

{

  super();

  this.geoPoint?=?geoPoint;

  this.context?=?context;

  this.drawable?=?drawable;

  }

  @Override

  public?void?draw(Canvas?canvas,?MapView?mapView,?boolean?shadow)?{

  Projection?projection?=?mapView.getProjection();

  Point?point?=?new?Point();

  projection.toPixels(geoPoint,?point);

  Bitmap?bitmap?=?BitmapFactory.decodeResource(context.getResources(),

  drawable);

  canvas.drawBitmap(bitmap,?point.x-bitmap.getWidth()/2?,?point.y-bitmap.getHeight(),?null);

  super.draw(canvas,?mapView,?shadow);

  }

  }

  }

  效果图如下:

Android?通过地名获得经纬度并标识在地图上

  相关解决方案