谷歌方向API找不到我的API密钥

问题描述 投票:0回答:1

我正在开发一个安卓应用程序,其中一个客户请求一个工人的pickuplocation,所以我使用谷歌方向库来显示客户和工人之间的路线,但方向API一直抛出错误,说你必须使用一个API密钥,以使请求谷歌云平台,已经我已经创建了一个项目在谷歌云控制台和生成我的密钥。 这里是我做了什么;首先我开始我的项目没有计费帐户,然后在我的项目过程中,我被要求创建一个计费帐户,以使方向请求,所以我链接我的项目WhatWhat我做了;我添加我的API密钥到manifesti已经启用地方API和方向API在方向部分,我使用谷歌方向库,这onei添加它到应用程序构建-gradle

编译'com.github.jd-alexander:library:1.1.0'。

这里是我的活动中的代码示例,用于方向请求,因为我使用了上面的库,我不知道是不是因为我创建了项目,然后又创建了计费账户,可能是什么问题?连地方API都不能正常使用

private void getRouteToMarker(LatLng customerpickuplocation) {
Routing routing = new Routing.Builder()
            .travelMode(AbstractRouting.TravelMode.DRIVING)
            .withListener(this)
            .alternativeRoutes(false)
            .waypoints(new LatLng(lat, lng), customerpickuplocation)
            .build();
    routing.execute();
}
@Override
public void onRoutingFailure(RouteException e) {
    if (e != null) {
        Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "Something went wrong, Try again", Toast.LENGTH_SHORT).show();
    }
}
@Override
public void onRoutingSuccess(ArrayList<Route> route, int shortestRouteIndex) {
    if (polylines.size() > 0) {
        for (Polyline poly : polylines) {
            poly.remove();
        }
    }

    polylines = new ArrayList<>();
    //add route(s) to the map.
    for (int i = 0; i < route.size(); i++) {

        //In case of more than 5 alternative routes
        int colorIndex = i % COLORS.length;

        PolylineOptions polyOptions = new PolylineOptions();
        polyOptions.color(getResources().getColor(COLORS[colorIndex]));
        polyOptions.width(10 + i * 3);
        polyOptions.addAll(route.get(i).getPoints());
        Polyline polyline = mMap.addPolyline(polyOptions);
        polylines.add(polyline);
        Toast.makeText(getApplicationContext(), "Route " + (i + 1) + ": distance - " + route.get(i).getDistanceValue() + ": duration - " + route.get(i).getDurationValue(), Toast.LENGTH_SHORT).show();
    }
}
android google-maps
1个回答
0
投票

我想出了一个解决方案,但不是一个很好的安全措施,通过添加密钥的getRouteToMarker()方法

private void getRouteToMarker(LatLng customerpickuplocation) {
    Routing routing = new Routing.Builder()
                .travelMode(AbstractRouting.TravelMode.DRIVING)
                .withListener(this)
                .alternativeRoutes(false)
                .waypoints(new LatLng(lat, lng), customerpickuplocation) 
                .key("your api key")
                .build();
        routing.execute();

}
© www.soinside.com 2019 - 2024. All rights reserved.