android - 使用 map 查找特定的附近地点

标签 android google-maps

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

我试过这段代码:MapActivity query for nearest hospital/restaurant not working

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

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

{ "error_message" : "This API project is not authorized to use this API. Please ensure this API is activated in the Google Developers Console: https://console.developers.google.com/apis/api/places_backend?project=_", "html_attributions" : [], "results" : [], "status" : "REQUEST_DENIED" }

我该如何解决?

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

最佳答案

我认为你的 api key 不匹配

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

  1. 首先,您应该在开发人员控制台中为 Web 服务启用 Google Place API。它列在 Google Maps APIs

enter image description here

  1. 如果您想从浏览器测试您的 api,您不应该对 api key 设置任何限制..选择无限制..

  2. 同时使用此链接测试您附近的地点 api:我认为您的 url 中缺少某些内容

使用这个: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);

        }
    }
}

最后使用方法as..

StringBuilder sbValue = new StringBuilder(sbMethod());
PlacesTask placesTask = new PlacesTask();
placesTask.execute(sbValue.toString());

关于android - 使用 map 查找特定的附近地点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41363480/

相关文章:

javascript - 文本搜索多个位置

java - 如何在谷歌地图上添加和检索地点

android listview 快速滚动自定义问题

安卓/谷歌播放 : do I really need my OWN server to manage inapp billing subscriptions?

android - 如何使用 CursorWrapper 过滤 Cursor 行

javascript - 使用登录的 Google map 绘制标记和保存标记时出现问题

css - 在 Google map 上 float 一个 div

java - 在日历android中设置月份的日期

java - Eclipse Java堆空间

android - 如何为 Android 生成新的和第二个 Google Maps API key ?