简体   繁体   English

谷歌最近一次更新后,Direction API谷歌地图出现问题

[英]Problem with Direction API google map after the last update from google

I need to build a direction in my android application so 1- i get new key from google api console 2- i did the billing and got free trial 3- i follow the instruction in google developers but i have a problem Invalid Api key i note that there is a missing part in my work which is step two in google guide Step 2: Add the API key to your app When loading the Directions API, substitute YOUR_API_KEY in the code below with the API key you got from the previous step https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=YOUR_API_KEY but i don't know where to put this code ? 我需要在我的android应用程序中建立一个方向,所以1-我从Google api控制台获取了新密钥2-我做了账单并获得了免费试用版3-我按照Google开发人员的说明进行操作,但是我遇到了问题,我注意到无效的Api密钥我的工作中缺少一部分,这是Google指南中的第二步。步骤2:将API密钥添加到您的应用中加载Directions API时,将下面代码中的YOUR_API_KEY替换为您从上一步 https 获得的API密钥 : //maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=YOUR_API_KEY,但我不知道将此代码放在哪里? and finally this is my all code : 最后这是我的全部代码:

    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;    }

You are messing the Javascript API of Google Directions with the Map SDK for Android that you're using. 您将Google Directions的Javascript API与使用的Android Map SDK混淆了。

Check out the doc here: https://developers.google.com/maps/documentation/android-sdk/signup 在此处查看文档: 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: 在AndroidManifest.xml中,将以下元素添加到该元素的子元素中,方法是将其插入在结束标记之前:

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

[SOLVED] this was the reason [解决]这是原因

            .setApiKey("@string/google_maps_key")

it take this as string not the google api key so t 它把它作为字符串而不是谷歌API密钥所以

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM