Google maps API in Android

左心房为你撑大大i 提交于 2019-12-08 06:14:58

问题


I am trying to implement a functionality such that when a user enters an address in a text field and clicks on a button, that address should be copied into the destination field of Google maps and the user location should be set to "My Location" (his current location).


回答1:


In order to show route on Google Map, Just call an intent which passes current and destination latitude and longitude. You may also pass address in any case if don't know lat-long. After doing this, its Google Map's job to show location. You may also show street view.

In below code, there are three parameters : current_lat, current_longi, dest_address

 final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?" 
+ "saddr="+ current_lat+","+current_longi + "&daddr="+dest_address ));

intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");

startActivity(intent); 

If you have current address and destination address, then you can write like this :

Uri.parse("http://maps.google.com/maps?" 
+ "saddr="+curr_address+ "&daddr="+dest_address ));

If you have current and destination latitude and longitude both then you can write like this :

Uri.parse("http://maps.google.com/maps?" 
+ "saddr="+ current_lat+","+current_longi + "&daddr="+ destt_lat+","+dest_longi  ));

When you call this intent, Google Map shows option whether to draw route by bus or by walk.




回答2:


You can open google map in following way:

String CURRENT_LOCATION = "current_location_latitude, current_location_longitude";
String DESTINATION_LOCATION = "address_from_your_text_view";

Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+ CURRENT_LOCATION +" daddr="+DESTINATION_LOCATION));
startActivity(intent);

Note: You should get device/user current location from LocationManager



来源:https://stackoverflow.com/questions/15217831/google-maps-api-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!