当前位置:嗨网首页>书籍在线阅读

06-我要导航

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

20.2.3 我要导航

导航算法比较复杂,可以使用Google地图或百度地图提供的现成路径规划功能。本系统采用Google地图API。当用户确定开始导航的时候首先调用getLocationProvider()取得现有的Location,以此取得目前所在位置的地理坐标(fromPoint),并获取用户输入的地址坐标(toPoint),然后再调用内置地图程序,路径规划Intent要打开的其实就是URL类型,以Uri.Parse()传入Google Map路线规划的地址:http://maps.google.com/maps?f=d,表示要使用Google Map的路径规划功能。导航关键代码如下。

public class WayActivity extends MapActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.waylayout); String defaultAddress="taipei"; mEditText01=(EditText)findViewById(R.id.MapeditText); mEditText01.setText(getGeoByAddress(defaultAddress).toString()); /创建MapView对象/ mMapView01=(MapView)findViewById(R.id.mapview2); mMapController01=mMapView01.getController(); /设置MapView的显示选项(交通)/ mMapView01.setTraffic(true); /放大的层数/ intZoomLevel=15; mMapController01.setZoom(intZoomLevel); /创建LocationManager对象取得系统LOCATION服务/ mLocationManager01=(LocationManager)getSystemService(Context.LOCATION_SERVICE); /*

  • 自定义函数,访问Location Provider,并将之保存在strLocationProvider当中 / getLocationProvider(); /传入Location对象,显示于MapView/ fromGeoPoint=getGeoByLocation(mLocation01); refreshMapViewByGeoPoint(fromGeoPoint, mMapView01,intZoomLevel); /
  • 创建LocationManager对象,监听 Location更改时事件,更新MapView / mLocationManager01.requestLocationUpdates (strLocationProvider, 2000, 10, mLocationListener01); mButton01=(Button)findViewById(R.id.btnsetway); mButton01.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if(mEditText01.getText().toString()!="") { /取得User要前往地址的GeoPoint对象/ toGeoPoint= getGeoByAddress(mEditText01.getText().toString()); /线路规划Intent/ Intent intent=new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); /传入路径规划所需要的地标地址/ intent.setData ( Uri.parse("http://maps.google.com/maps?f=d&aaddr="+
 GeoPointToString(fromGeoPoint)+ "&daddr="+GeoPointToString(toGeoPoint)+ "&h1=cn") ); startActivity(intent); } } }); private final LocationListener mLocationListener01 = new LocationListener() { public void onLocationChanged(Location location) { /当手机收到位置更改时,将location传入getMyLocation/ mLocation01=location; fromGeoPoint=getGeoByLocation(location); refreshMapViewByGeoPoint(fromGeoPoint,mMapView01,intZoomLevel); } public void onProviderDisabled(String provider) { mLocation01=null; } }; /传入Location对象,取回其GeoPoint对象/ private GeoPoint getGeoByLocation(Location location) { GeoPoint gp=null; try { /当Location存在/ if(location!=null) { double geoLatitude=location.getLatitude()1E6; double geoLongitude=location.getLongitude()1E6; gp=new GeoPoint((int)geoLatitude,(int)geoLongitude); } } catch(Exception e) { e.printStackTrace(); } return gp; } /输入地址,取得其GeoPoint对象/ private GeoPoint getGeoByAddress(String strSearchAddress) { GeoPoint gp=null; try { if(strSearchAddress!="") { Geocoder mGeocoder01=new Geocoder (WayActivity.this,Locale.getDefault()); List
    lstAddress=mGeocoder01.getFromLocationName (strSearchAddress,1); if(!lstAddress.isEmpty()) { Address adsLocation=lstAddress.get(0); double geoLatitude=adsLocation.getLatitude()1E6; double geoLongitude=adsLocation.getLongitude()1E6; gp=new GeoPoint((int)geoLatitude,(int)geoLongitude); } } } catch(Exception e) { e.printStackTrace(); } return gp; } /传入geoPoint更新MapView里的Google Map/ private void refreshMapViewByGeoPoint(GeoPoint gp, MapView mapview, int zoomLevel) { try { mapview.displayZoomControls(true); MapController myMC=mapview.getController(); myMC.animateTo(gp); myMC.setZoom(zoomLevel); mapview.setSatellite(false); } catch(Exception e) { e.printStackTrace(); } } /传入经纬度更新MapView里的Google Map/ public static void refreshMapViewByCode (double latitude, double longitude, MapView mapview, int zoomLevel) { try { GeoPoint p=new GeoPoint((int)latitude,(int)longitude); mapview.displayZoomControls(true); MapController myMC=mapview.getController(); myMC.animateTo(p); myMC.setZoom(zoomLevel); mapview.setSatellite(false); } catch(Exception e) { e.printStackTrace(); } } /将GeoPoint里的经纬度以String,String返回/ private String GeoPointToString(GeoPoint gp) { // TODO Auto-generated method stub String strReturn=""; try { /当Location存在/ if(gp!=null) { double geoLatitude=gp.getLatitudeE6()/1E6; double geoLongitude=gp.getLongitudeE6()/1E6; strReturn=String.valueOf(geoLatitude)+","+ String.valueOf(geoLongitude); } } catch(Exception e) { e.printStackTrace(); } return strReturn; } /取得LocationProvider/ private void getLocationProvider() { try { Criteria mCriteria01=new Criteria(); mCriteria01.setAccuracy(Criteria.ACCURACY_FINE); mCriteria01.setAltitudeRequired(false); mCriteria01.setBearingRequired(false); mCriteria01.setCostAllowed(true); mCriteria01.setPowerRequirement(Criteria.POWER_LOW); strLocationProvider= mLocationManager01.getBestProvider(mCriteria01,true); mLocation01=mLocationManager01.getLastKnownLocation(strLocationProvider); } catch(Exception e) { mTextView01.setText(e.toString()); e.printStackTrace(); } } }