使用地图查找附近的特定地点

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

大家早上好。 我正在尝试实现一项活动来查找用户附近的一些特定地点。

我尝试了以下代码:最近医院/餐厅的MapActivity查询不起作用

经过一些更改后,地图现在可以正常工作并显示用户的当前位置,但不显示地点。

我已激活 API 密钥和 Google 地图 API。当我粘贴网址(https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-15,287,-47.33&radius=5000000&types=restaurant&sensor=true&key=AIzaSyCTZFZc7DBdk*)时,我收到消息:

{ "error_message" : "此 API 项目无权使用此 API。请确保在 Google 开发者控制台中激活此 API:https://console.developers.google.com/apis/api/places_backend?project=_ “, “html_attributions”:[], “结果” : [], “状态”:“REQUEST_DENIED” }

我该如何修复它?

编辑:我的控制台是这样的: APIS API_KEY

sffsfsdfsdf

android google-maps
1个回答
1
投票

我认为您的 api 密钥不匹配

看到你的屏幕截图后......你应该做几件事:

  1. 首先,您应该在开发者控制台中为Web服务启用Google place api..它列在
    Google Maps APIs

enter image description here

  1. 如果你想从浏览器测试你的API,你不应该对API密钥施加任何限制..在限制中选择无..

  2. 还可以使用此链接测试您附近的地点 api:我认为您的网址中缺少某些内容

使用此:https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&key=[your_api_key]

以下是Android中附近地点的简单示例。首先,生成 API 的查询字符串:

  public StringBuilder sbMethod() {

    //use your current location here
    double mLatitude = 37.77657;
    double mLongitude = -122.417506;

    StringBuilder sb = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
    sb.append("location=" + mLatitude + "," + mLongitude);
    sb.append("&radius=5000");
    sb.append("&types=" + "restaurant");
    sb.append("&sensor=true");
    sb.append("&key=******* YOUR API KEY****************");

    Log.d("Map", "api: " + sb.toString());

    return sb;
}

这里是用于查询 Places API 的

AsyncTask

 private class PlacesTask extends AsyncTask<String, Integer, String> {

    String data = null;

    // Invoked by execute() method of this object
    @Override
    protected String doInBackground(String... url) {
        try {
            data = downloadUrl(url[0]);
        } catch (Exception e) {
            Log.d("Background Task", e.toString());
        }
        return data;
    }

    // Executed after the complete execution of doInBackground() method
    @Override
    protected void onPostExecute(String result) {
        ParserTask parserTask = new ParserTask();

        // Start parsing the Google places in JSON format
        // Invokes the "doInBackground()" method of the class ParserTask
        parserTask.execute(result);
    }
}

这是

downloadURL
() 方法:

    private String downloadUrl(String strUrl) throws IOException {
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;
    try {
        URL url = new URL(strUrl);

        // Creating an http connection to communicate with url
        urlConnection = (HttpURLConnection) url.openConnection();

        // Connecting to url
        urlConnection.connect();

        // Reading data from url
        iStream = urlConnection.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

        StringBuffer sb = new StringBuffer();

        String line = "";
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        data = sb.toString();

        br.close();

    } catch (Exception e) {
        Log.d("Exception while downloading url", e.toString());
    } finally {
        iStream.close();
        urlConnection.disconnect();
    }
    return data;
}

ParserTask
用于解析 JSON 结果:

    private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String, String>>> {

    JSONObject jObject;

    // Invoked by execute() method of this object
    @Override
    protected List<HashMap<String, String>> doInBackground(String... jsonData) {

        List<HashMap<String, String>> places = null;
        Place_JSON placeJson = new Place_JSON();

        try {
            jObject = new JSONObject(jsonData[0]);

            places = placeJson.parse(jObject);

        } catch (Exception e) {
            Log.d("Exception", e.toString());
        }
        return places;
    }

    // Executed after the complete execution of doInBackground() method
    @Override
    protected void onPostExecute(List<HashMap<String, String>> list) {

        Log.d("Map", "list size: " + list.size());
        // Clears all the existing markers;
        mGoogleMap.clear();

        for (int i = 0; i < list.size(); i++) {

            // Creating a marker
            MarkerOptions markerOptions = new MarkerOptions();

            // Getting a place from the places list
            HashMap<String, String> hmPlace = list.get(i);


            // Getting latitude of the place
            double lat = Double.parseDouble(hmPlace.get("lat"));

            // Getting longitude of the place
            double lng = Double.parseDouble(hmPlace.get("lng"));

            // Getting name
            String name = hmPlace.get("place_name");

            Log.d("Map", "place: " + name);

            // Getting vicinity
            String vicinity = hmPlace.get("vicinity");

            LatLng latLng = new LatLng(lat, lng);

            // Setting the position for the marker
            markerOptions.position(latLng);

            markerOptions.title(name + " : " + vicinity);

            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));

            // Placing a marker on the touched position
            Marker m = mGoogleMap.addMarker(markerOptions);

        }
    }
}

最后使用方法为..

StringBuilder sbValue = new StringBuilder(sbMethod());
PlacesTask placesTask = new PlacesTask();
placesTask.execute(sbValue.toString());
© www.soinside.com 2019 - 2024. All rights reserved.