可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm designing one application in which I want to show specific location on Map.
I'm passing String
of address which is already placed on Google Map
.
Following is my Intent
code..
String url = "http://maps.google.com/maps?daddr="+address;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
But it gives me Google Map for getting direction. I know why that so, because I used daddr
in url
but I don't know what to use for specific location..Please tell me what to use there..
回答1:
I have not tested this but you could try :
First method:
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);
EDIT:
This might not work with Google maps 7,0
hence you could change the uri to :
Second option:
String geoUri = "http://maps.google.com/maps?q=loc:" + lat + "," + lng + " (" + mTitle + ")";
where mTitle
is the name of the location.
Third option:
geo:0,0?q=my+street+address
Fourth option:
String map = "http://maps.google.co.in/maps?q=" + yourAddress;
Hope that works and helps :D..
回答2:
Get Lat-Lng Using this web-service
http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false
Then Pass it to this code
String strUri = "http://maps.google.com/maps?q=loc:" + lat + "," + lng + " (" + "Label which you want" + ")";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(strUri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
I hope it will help you
Thank you.
回答3:
The latest version of map provides better solution. If you wish show an address on map use below code.
Uri mapUri = Uri.parse("geo:0,0?q=" + Uri.encode(address));
Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
If you want to display using latitude and longitude values, use below method.
Uri mapUri = Uri.parse("geo:0,0?q=lat,lng(label)");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
Lat and lng being the latitude and longitude you wish to display, the label here is optional.
回答4:
You should use something like this
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
startActivity(intent);
And to drop a pin
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:" + AppointmentDetailLayout.docLatitude
+ "," + AppointmentDetailLayout.docLongitude
+ "?q=" + AppointmentDetailLayout.docLatitude
+ "," + AppointmentDetailLayout.docLongitude
+ "(" + label + ")"));
intent.setComponent(new ComponentName(
"com.google.android.apps.maps",
"com.google.android.maps.MapsActivity"));
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
try {
context.startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("market://details?id=com.google.android.apps.maps")));
} catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=com.google.android.apps.maps")));
}
e.printStackTrace();
}
回答5:
This will work like a charm. Make sure to check for existence for Google Maps, so this can work universally across all non Google devices also. It will open in browser in such cases.
Also remember dont have www in the URL. Else this will not work.
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?q=loc:" + latitude + "," + longitude);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Only if initiating from a Broadcast Receiver
String mapsPackageName = "com.google.android.apps.maps";
if (Utility.isPackageExisted(context, mapsPackageName)) {
i.setClassName(mapsPackageName, "com.google.android.maps.MapsActivity");
i.setPackage(mapsPackageName);
}
context.startActivity(i);
回答6:
Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
Refer this documentation
回答7:
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=" + src_lat+ "," + src_lng + "&daddr=" + des_lat + "," + des_lng));
startActivity(intent);
}catch (ActivityNotFoundException ane){
Toast.makeText(activity, "Please Install Google Maps ", Toast.LENGTH_LONG).show();
}catch (Exception ex){
ex.getMessage();
}
}
});
回答8:
This helper method using uriBuilder for cleaner code and handle condition if there is no activity on device that can open map
public static boolean openMap(Context context, String address) {
Uri.Builder uriBuilder = new Uri.Builder()
.scheme("geo")
.path("0,0")
.appendQueryParameter("q", address);
Intent intent = new Intent(Intent.ACTION_VIEW, uriBuilder.build());
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
return true;
}
return false;
}