[Android]_ch9.13_GoogleMap導航功能
n導航功能
n透過HTTP協定傳送出發地與目的地資料,Google地圖伺服器就可以計算出該走哪條路
nString uriStr = String.format("http://maps.google.com/maps?f=d&saddr=%f,%f&daddr=%f,%f",fromLat, fromLng, toLat, toLng);
n
n可以將導航結果以內建的Google地圖應用程式來顯示
nIntent intent = new Intent();
nintent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
nintent.setAction(android.content.Intent.ACTION_VIEW);
nintent.setData(Uri.parse(uriStr));
nstartActivity(intent);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name=et1.getText().toString();
Geocoder gc=new Geocoder(MainActivity.this);
List<Address> list=null;
Address address=null;
try {
list=gc.getFromLocationName(name, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(list==null || list.isEmpty())
Toast.makeText(MainActivity.this, "找不到該位置!", Toast.LENGTH_SHORT).show();
else{
address=list.get(0);
}
double startLatitude=myLocation.getLatitude();
double startLongitude=myLocation.getLongitude();
double endLatitude=address.getLatitude();
double endLongitude=address.getLongitude();
String uriStr = String.format("http://maps.google.com/maps?f=d&saddr=%f,%f&daddr=%f,%f",startLatitude, startLongitude, endLatitude, endLongitude);
Intent intent = new Intent();
intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse(uriStr));
startActivity(intent);
}
});
沒有留言:
張貼留言