gpt4 book ai didi

android - 如何在外部 Google map 应用程序中加载 Android Direction API 结果?

转载 作者:行者123 更新时间:2023-11-29 23:46:07 35 4
gpt4 key购买 nike

我正在使用 Google Directions API 获取带有路标的两个位置之间的路线。

目前我正在做的是使用 Direction API 获取两个位置之间的方向详细信息,并在我的应用程序中集成的 Google map 中显示结果。它按预期工作得很好。我是这样做的:

    private DirectionsResult getDirectionsDetails(String origin,String destination,TravelMode mode) {
Log.i("testtt"," Origin "+origin+" Destination "+destination);
DateTime now = new DateTime();
try {
return DirectionsApi.newRequest(getGeoContext())
.mode(mode)
.origin(origin)
.waypoints(waypoints.toArray(new String[0]))
.destination(destination)
.departureTime(now)
.await();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (com.google.maps.errors.ApiException e) {
e.printStackTrace();
return null;
}
}

@Override
public void onMapReady(GoogleMap googleMap) {

setupGoogleMapScreenSettings(googleMap);
DirectionsResult results = getDirectionsDetails(origin,destination,TravelMode.DRIVING);
if (results != null) {
addPolyline(results, googleMap);
positionCamera(results.routes[overview], googleMap);
addMarkersToMap(results, googleMap);
}
}

private void setupGoogleMapScreenSettings(GoogleMap mMap) {
mMap.setBuildingsEnabled(true);
mMap.setIndoorEnabled(true);
mMap.setTrafficEnabled(true);
UiSettings mUiSettings = mMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
mUiSettings.setCompassEnabled(true);
mUiSettings.setMyLocationButtonEnabled(true);
mUiSettings.setScrollGesturesEnabled(true);
mUiSettings.setZoomGesturesEnabled(true);
mUiSettings.setTiltGesturesEnabled(true);
mUiSettings.setRotateGesturesEnabled(true);
}

private void addMarkersToMap(DirectionsResult results, GoogleMap mMap) {
mMap.addMarker(new MarkerOptions().position(new LatLng(results.routes[overview].legs[overview].startLocation.lat,results.routes[overview].legs[overview].startLocation.lng)).title(results.routes[overview].legs[overview].startAddress));
mMap.addMarker(new MarkerOptions().position(new LatLng(results.routes[overview].legs[overview].endLocation.lat,results.routes[overview].legs[overview].endLocation.lng)).title(results.routes[overview].legs[overview].endAddress).snippet(getEndLocationTitle(results)));
}

private void positionCamera(DirectionsRoute route, GoogleMap mMap) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(route.legs[overview].startLocation.lat, route.legs[overview].startLocation.lng), 12));
}

private void addPolyline(DirectionsResult results, GoogleMap mMap) {
List<LatLng> decodedPath = PolyUtil.decode(results.routes[overview].overviewPolyline.getEncodedPath());
mMap.addPolyline(new PolylineOptions().addAll(decodedPath));
}

但我想要的是我想在外部谷歌地图应用程序中加载这个方向结果。我想问的是,有什么方法可以通过 Intent 将 DirectionsResult 对象传递给 Google Maps 应用程序,以便它在应用程序中显示路线。我想要这个的原因是为了避免将 Google Maps API 集成到项目中,因为它不再是完全免费的。 Pricing details

最佳答案

正如我在您的代码中看到的那样,您没有对 DirectionsApi 给出的 DirectionsDetails 执行任何计算,您可以在谷歌地图中打开您的位置坐标。

默认情况下,Google map 始终根据当前路况和时间加载最佳可用路线:

String geoUri = "http://maps.google.com/maps?q=loc:" + lat + "," + lng + " (" + mTitle + ")";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri));
context.startActivity(intent);

如果您想在源和目的地之间添加航路点,您可以查看this answer。 .

如果需要,您可以通过 DirectionsApi 找到路线并处理内部分析数据,例如。在他的位置和新位置之间旅行的大约时间、距离等。

关于android - 如何在外部 Google map 应用程序中加载 Android Direction API 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51380283/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com