分享

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

 ruiruiruiruichen 2012-12-20

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

2011-9-21 09:05|发布者: benben|查看: 787|评论: 0

摘要: Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 ...

前言:

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

 

 

效果图:

 

 

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, longminTime, float minDistance, LocationListenerlistener)

:对指定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

View Code
public class ATMDynamicItemizedOverlay extends ItemizedOverlay<OverlayItem>{

private ArrayList<OverlayItem> items ;

public ATMDynamicItemizedOverlay(Drawable defaultMarker)
{
super(boundCenterBottom(defaultMarker));
items
=new ArrayList<OverlayItem>();
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 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()
{
public void 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);

List
<Overlay> 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 onKeyDown(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 onLocationChanged(Location location) {
UpdateMapView(location);
Log.d(
"Location", "onLocationChanged");
}

//location is Disable
@Override
public void onProviderDisabled(String provider) {
Log.d(
"Location", "onProviderDisabled");
}

//location is enabled
@Override
public void onProviderEnabled(String provider) {
Log.d(
"Location", "onProviderEnabled");
}

//location's status changes
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(
"Location", "onStatusChanged");
}
};

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多