前言:
下面讲述在MapView中显示当前位置,并同时已知的银行ATM机的位置
效果图:
1 实现效果说明:
实现显示当前位置和银行ATM机的位置,主要分以下两个步骤,
1) 根据位置服务找到当前的GPS坐标
2) 将当前坐标和ATM坐标,用图层的方式显示到地图上
2 根据位置服务找到当前的GPS坐标
l 设置应用的权限
在AndroidManifest.xml文件中设置ACCESS_FINE_LOCATION权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- 获取 定位服务
String context=Context.LOCATION_SERVICE;
locationManager=(LocationManager)getSystemService(context);
- 根据定位服务获取位置
定位服务接口
[1] LocationManager.getLastKnownLocation(provider)
注:获取最后一次定位坐标信息
[2] void android.location.LocationManager.requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
注:对指定provider进行侦听,条件为 最小时间minTime ,最小距离minDistance
3 根据位置服务找到当前的GPS坐标
- 添加MyLocationOverlay层,显示当前位置.
List<Overlay> overlays=map_view.getOverlays(); // set location Overlay
MyLocationOverlay mylocationOver=new MyLocationOverlay(this,map_view);
overlays.add(mylocationOver);
mylocationOver.enableCompass(); //显示指南针
mylocationOver.enableMyLocation(); //允许当前位置
- 自定义层,显示ATM位置
集成ItemizedOverlay<OverlayItem>类
调用方式:
atm=new ATMDynamicItemizedOverlay(this.getResources().getDrawable(R.drawable.map_atm));
overlays.add(atm);
atm.addNewItem(new GeoPoint(lat.intValue()+1000,lng.intValue()), "marketText", "snippet");
4 源代码:
自定义 ATMDynamicItemizedOverlay 类
publicclass ATMDynamicItemizedOverlay extends ItemizedOverlay{ private ArrayList items ; public ATMDynamicItemizedOverlay(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); items=new ArrayList (); populate(); } publicvoid addNewItem(GeoPoint location,String marketText,String snippet) { items.add(new OverlayItem(location,marketText,snippet)); populate(); } publicvoid removeItem(int index) { items.remove(index); populate(); } @Override protected OverlayItem createItem(int index) { // TODO Auto-generated method stub return items.get(index); } @Override publicint size() { // TODO Auto-generated method stub return items.size(); } }
窗体文件
publicclass LocationMap extends MapActivity { LocationManager locationManager; MapView map_view; ATMDynamicItemizedOverlay atm; @Override publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.locationmap); //add bank button onclick event ImageView imgView_back=(ImageView)findViewById(R.id.map_imgView_back); imgView_back.setOnClickListener(new ImageView.OnClickListener() { publicvoid onClick(View v){ ViewUtility.NavigateActivate(LocationMap.this,Main.class ); } }); //set MapControl map_view =(MapView)findViewById(R.id.map_view); map_view.setStreetView(true); map_view.setTraffic(true); map_view.setBuiltInZoomControls(false); map_view.setSatellite(false); Listoverlays=map_view.getOverlays(); // set location Overlay MyLocationOverlay mylocationOver=new MyLocationOverlay(this,map_view); overlays.add(mylocationOver); mylocationOver.enableCompass(); mylocationOver.enableMyLocation(); //set atm Overlay atm=new ATMDynamicItemizedOverlay(this.getResources().getDrawable(R.drawable.map_atm)); overlays.add(atm); String context=Context.LOCATION_SERVICE; locationManager=(LocationManager)getSystemService(context); String provider=LocationManager.GPS_PROVIDER; //OverlayItem a=new OverlayItem(null, provider, provider; Location location =locationManager.getLastKnownLocation(provider); if(location!=null) { UpdateMapView(location); } locationManager.requestLocationUpdates(provider, 10000, 5, locationListener ); } /* By new location ,update MapWiew's Label */ privatevoid UpdateMapView(Location location) { MapController mapcontroller=map_view.getController(); Double lat=location.getLatitude()*1E6; Double lng=location.getLongitude()*1E6; GeoPoint point=new GeoPoint(lat.intValue(),lng.intValue()); mapcontroller.setCenter(point); mapcontroller.setZoom(20); mapcontroller.animateTo(point); atm.addNewItem(new GeoPoint(lat.intValue()+1000,lng.intValue()), "marketText", "snippet"); Toast.makeText(this, "lat:"+ String.valueOf(lat.intValue())+"lng:"+String.valueOf(lng.intValue()), Toast.LENGTH_SHORT).show(); } @Override publicboolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK){ ViewUtility.NavigateActivate(LocationMap.this, Main.class); } returnfalse; } @Override protectedboolean isRouteDisplayed() { // TODO Auto-generated method stub returnfalse; } //create location listener private LocationListener locationListener =new LocationListener(){ //location is changed @Override publicvoid onLocationChanged(Location location) { UpdateMapView(location); Log.d("Location", "onLocationChanged"); } //location is Disable @Override publicvoid onProviderDisabled(String provider) { Log.d("Location", "onProviderDisabled"); } //location is enabled @Override publicvoid onProviderEnabled(String provider) { Log.d("Location", "onProviderEnabled"); } //location's status changes @Override publicvoid onStatusChanged(String provider, int status, Bundle extras) { Log.d("Location", "onStatusChanged"); } }; }