(android 地图实战开发)3 在地图上显示当前位置和自定义银行位置


前言:
下面讲述在MapView中显示当前位置,并同时已知的银行ATM机的位置


(android 地图实战开发)3 在地图上显示当前位置和自定义银行位置
效果图:


1 实现效果说明:
实现显示当前位置和银行ATM机的位置,主要分以下两个步骤,
1) 根据位置服务找到当前的GPS坐标

2) 将当前坐标和ATM坐标,用图层的方式显示到地图上

2 根据位置服务找到当前的GPS坐标
l 设置应用的权限
在AndroidManifest.xml文件中设置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 overlays=map_view.getOverlays(); // set location Overlay
MyLocationOverlay mylocationOver=new MyLocationOverlay(this,map_view);
overlays.add(mylocationOver);
mylocationOver.enableCompass(); //显示指南针
mylocationOver.enableMyLocation(); //允许当前位置

  • 自定义层,显示ATM位置

集成ItemizedOverlay
调用方式:
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 类
(android 地图实战开发)3 在地图上显示当前位置和自定义银行位置(android 地图实战开发)3 在地图上显示当前位置和自定义银行位置View Code public class ATMDynamicItemizedOverlay extends ItemizedOverlay{ private ArrayList items ; public ATMDynamicItemizedOverlay(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); items=new ArrayList(); populate(); } public void addNewItem(GeoPoint location,String marketText,String snippet) { items.add(new OverlayItem(location,marketText,snippet)); populate(); } public void removeItem(int index) { items.remove(index); populate(); } @Override protected OverlayItem createItem(int index) { // TODO Auto-generated method stub return items.get(index); } @Override public int size() { // TODO Auto-generated method stub return items.size(); } }

窗体文件

View Code public class LocationMap extends MapActivity { LocationManager locationManager; MapView map_view; ATMDynamicItemizedOverlay atm; @Override public void _disibledevent=>super.onCreate(savedInstanceState); setContentView(R.layout.locationmap); //add bank button _disibledevent=> ImageView imgView_back=(ImageView)findViewById(R.id.map_imgView_back); imgView_back.setOnClickListener(new ImageView.OnClickListener() { public void _disibledevent=>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); List overlays=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 */ private void 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 public boolean _disibledevent=>int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK){ ViewUtility.NavigateActivate(LocationMap.this, Main.class); } return false; } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } //create location listener private LocationListener locationListener = new LocationListener(){ //location is changed @Override public void _disibledevent=>"Location", "onLocationChanged"); } //location is Disable @Override public void _disibledevent=>"Location", "onProviderDisabled"); } //location is enabled @Override public void _disibledevent=>"Location", "onProviderEnabled"); } //location's status changes @Override public void _disibledevent=>int status, Bundle extras) { Log.d("Location", "onStatusChanged"); } }; }

特别感谢:卓易点评网Anroid Market 全新体验)赞助支持!!!

Tags: 

延伸阅读

最新评论

发表评论