String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?saddr=%f,%f(%s)&daddr=%f,%f (%s)", sourceLatitude, sourceLongitude, "Home Sweet Home", destinationLatitude, destinationLongitude, "Where the party is at");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
要使用当前位置作为起点(不幸的是,我还没有找到一种方法来标记当前位置),然后使用以下方法
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=%f,%f (%s)", destinationLatitude, destinationLongitude, "Where the party is at");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:37.827500,-122.481670"));
i.setClassName("com.google.android.apps.maps",
"com.google.android.maps.MapsActivity");
startActivity(i);
String addressString = "1600 Amphitheatre Parkway, CA";
/*
Build the uri
*/
Uri.Builder builder = new Uri.Builder();
builder.scheme("geo")
.path("0,0")
.query(addressString);
Uri addressUri = builder.build();
/*
Intent to open the map
*/
Intent intent = new Intent(Intent.ACTION_VIEW, addressUri);
/*
verify if the devise can launch the map intent
*/
if (intent.resolveActivity(getPackageManager()) != null) {
/*
launch the intent
*/
startActivity(intent);
}
const val GOOGLE_MAPS_APP = "com.google.android.apps.maps"
const val HUAWEI_MAPS_APP = "com.huawei.maps.app"
fun openMap(lat:Double,lon:Double) {
val packName = if (isHmsOnly(context)) {
HUAWEI_MAPS_APP
} else {
GOOGLE_MAPS_APP
}
val uri = Uri.parse("geo:$lat,$lon?q=$lat,$lon")
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.setPackage(packName);
if (intent.resolveActivity(context.packageManager) != null) {
context.startActivity(intent)
} else {
openMapOptions(lat, lon)
}
}
private fun openMapOptions(lat: Double, lon: Double) {
val intent = Intent(
Intent.ACTION_VIEW,
Uri.parse("geo:$lat,$lon?q=$lat,$lon")
)
context.startActivity(intent)
}
HMS检查:
private fun isHmsAvailable(context: Context?): Boolean {
var isAvailable = false
if (null != context) {
val result =
HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context)
isAvailable = ConnectionResult.SUCCESS == result
}
return isAvailable}
private fun isGmsAvailable(context: Context?): Boolean {
var isAvailable = false
if (null != context) {
val result: Int = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context)
isAvailable = com.google.android.gms.common.ConnectionResult.SUCCESS == result
}
return isAvailable }
fun isHmsOnly(context: Context?) = isHmsAvailable(context) && !isGmsAvailable(context)
Uri uri = Uri.parse("google.navigation:q=" + Latitude + "," + longitude + "&mode=d");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);