有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

谷歌最近一次更新后,谷歌地图的方向API出现java问题

我需要在我的安卓应用程序中建立一个方向,以便 1-我从谷歌api控制台获得新密钥 2-我做了账单,得到了免费试用 3-我遵循谷歌开发者的指示,但我有一个问题 无效的Api密钥我注意到我的工作中缺少了一部分,那就是谷歌指南中的第二步 步骤2:将API密钥添加到应用程序 加载Directions API时,将下面代码中的API密钥替换为从上一步获得的API密钥https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=YOUR_API_KEY 但我不知道该把代码放在哪里? 最后,这是我的全部代码:

    DateTime now = new DateTime();
        try {
            DirectionsResult result = DirectionsApi.newRequest(getGeoContext())
                    .mode(TravelMode.DRIVING).origin(String.valueOf(userLocation))
                    .destination(String.valueOf(sydney)).departureTime(now)
                    .await();
            addMarkersToMap(result,mMap);
            addPolyline(result,mMap);


        } catch (ApiException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
  private GeoApiContext getGeoContext() {
    GeoApiContext geoApiContext = new GeoApiContext();
    return geoApiContext.setQueryRateLimit(3)
            .setApiKey("@string/google_maps_key")
            .setConnectTimeout(1, TimeUnit.SECONDS)
            .setReadTimeout(1, TimeUnit.SECONDS)
            .setWriteTimeout(1, TimeUnit.SECONDS);
}
private void addMarkersToMap(DirectionsResult results, GoogleMap mMap) {
    mMap.addMarker(new MarkerOptions().position(new LatLng(results.routes[0].legs[0].startLocation.lat,results.routes[0].legs[0].startLocation.lng)).title(results.routes[0].legs[0].startAddress));
    mMap.addMarker(new MarkerOptions().position(new LatLng(results.routes[0].legs[0].endLocation.lat,results.routes[0].legs[0].endLocation.lng)).title(results.routes[0].legs[0].startAddress)
            .snippet(getEndLocationTitle(results)));
}
private void addPolyline(DirectionsResult results, GoogleMap mMap) {
    List<LatLng> decodedPath = PolyUtil.decode(results.routes[0].overviewPolyline.getEncodedPath());
    mMap.addPolyline(new PolylineOptions().addAll(decodedPath));
}
private String getEndLocationTitle(DirectionsResult results){
    return  "Time :"+ results.routes[0].legs[0].duration.humanReadable + " Distance :" + results.routes[0].legs[0].distance.humanReadable;    }

共 (2) 个答案

  1. # 1 楼答案

    [已解决] 这就是原因

                .setApiKey("@string/google_maps_key")
    

    它将此作为字符串,而不是GoogleAPI键 所以t

  2. # 2 楼答案

    您正在将Google Directions的Javascript API与您正在使用的Android地图SDK弄混

    在这里查看文档:https://developers.google.com/maps/documentation/android-sdk/signup

    In AndroidManifest.xml, add the following element as a child of the element, by inserting it just before the closing tag:

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="YOUR_API_KEY"/>