我有一个 Android 应用程序,它在地图上显示了许多位置。当我点击一个位置时,我想将位置的纬度和设备位置的经度传递给谷歌地图应用程序,以便它可以向我显示两者之间的路线信息。
这可能吗?如果有怎么办?谢谢。
我有一个 Android 应用程序,它在地图上显示了许多位置。当我点击一个位置时,我想将位置的纬度和设备位置的经度传递给谷歌地图应用程序,以便它可以向我显示两者之间的路线信息。
这可能吗?如果有怎么办?谢谢。
是的,您可以将 lat 和 lng 传递给 Maps ,并使用 Android Intent 显示前往该地点的路线。
方向总是从用户当前位置给出。
以下查询将帮助您执行该操作。您可以在此处传递目的地纬度和经度:
google.navigation:q=latitude,longitude
使用如上:
Uri gmmIntentUri = Uri.parse("google.navigation:q=latitude,longitude");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
更多信息:https ://developers.google.com/maps/documentation/urls/android-intents
截至 2017 年,谷歌推荐的方法是谷歌地图 URL:
https://developers.google.com/maps/documentation/urls/guide#directions-action
您可以按照文档为 Google 路线创建跨平台通用 URL,并在您的意图中使用该 URL。例如,URL 可能类似于
https://www.google.com/maps/dir/?api=1&destination=60.626200,16.776800&travelmode=driving
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
{
@Override
public void onMapClick(LatLng latLng)
{
String url = "https://www.google.com/maps/dir/?api=1&destination=" + latLng.latitude + "," + latLng.longitude + "&travelmode=driving";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
希望这可以帮助!
你可以试试这个:
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
{
@Override
public void onMapClick(LatLng latLng)
{
String url = "http://maps.google.com/maps?daddr=" + latLng.latitude + "," + latLng.longitude;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
添加依赖编译'com.google.android.gms:play-services-maps:11.0.4'
添加清单文件
<permission
android:name="com.example.com.locationmap.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> and meta data
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCHrUMMMFTLannfAaqQ9RGVaTu301Ee0j8" />
xml的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
java类的代码
public class MapActivity extends FragmentActivity implements
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private GoogleMap mMap;
double latitude = 0;
double longitude = 0;
double latitude2 = 0;
double longitude2 = 0;
LatLng position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
latitude = getIntent().getDoubleExtra("lat", 0);
longitude = getIntent().getDoubleExtra("long", 0);
// Getting Reference to SupportMapFragment of activity_map.xml
SupportMapFragment fm = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
fm.getMapAsync(MapActivity.this);
}
@Override
public void onConnected(Bundle bundle) {
/* getDirection();*/
/*getCurrentLocation();*/
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onMapReady(GoogleMap googleMap) {
// Receiving latitude from MainActivity screen
latitude = getIntent().getDoubleExtra("lat", 0);
// Receiving longitude from MainActivity screen
longitude = getIntent().getDoubleExtra("long", 0);
latitude2 = 73.998;
// Receiving longitude from MainActivity screen
longitude2 = 22.4567;
position = new LatLng(latitude, longitude);
// Instantiating MarkerOptions class
MarkerOptions options = new MarkerOptions();
// Setting position for the MarkerOptions
options.position(position);
// Setting title for the MarkerOptions
options.title("Location");
// Setting snippet for the MarkerOptions
// Adding Marker on the Google Map
try {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
String cityName = addresses.get(0).getAddressLine(0);
String stateName = addresses.get(0).getAddressLine(1);
String countryName = addresses.get(0).getAddressLine(2);
options.snippet(cityName + "," + stateName);
} catch (Exception ex) {
ex.getMessage();
options.snippet("Latitude:" + latitude + ",Longitude:" + longitude);
}
googleMap.addMarker(options);
googleMap.addMarker(options.position(new LatLng(latitude2,
longitude2)));
// Creating CameraUpdate object for position
CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);
// Creating CameraUpdate object for zoom
CameraUpdate updateZoom = CameraUpdateFactory.zoomTo(6);
// Updating the camera position to the user input latitude and longitude
googleMap.moveCamera(updatePosition);
// Applying zoom to the marker position
googleMap.animateCamera(updateZoom);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
/*
getMenuInflater().inflate(R.menu.main, menu);
*/
return true;
}
}
`