Android(安卓)通过 地名 获得 经纬度 并 标识 在地图上
16lz
2021-01-24
要是调用Geocoder的getFromLocationName(),该方法可以传入地名。
在使用该方法前需要geo = new Geocoder(this, Locale.CHINA);
不然在地图上是查询不到的。
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; /** * @author jason zhao * */ 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); Listaddresses = 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; /** * @author jason.zhao * */ 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); Listaddresses = 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 百度地图定位功能实现
- Android(安卓)获取照相机图片或本地图片
- android调用google地图
- Android(安卓)GPS
- 百度地图 android SDKv2.2.0
- Android游戏开发设计步骤
- 【Android】说做就做:都市列表+卫星地图
- Android百度地图之方向感应和模式更改
- 关于百度地图只显示中间部分,显示不全的原因