首页 文章

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

提问于
浏览
1

各位早上好 . 我正在尝试实施一项活动来查找用户附近的某些特定地点 .

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

现在一些更改后, Map 正在工作并显示用户的当前位置,但不显示地点 .

我已激活API KEY和Google Maps 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 Developers Console中激活此API:https://console.developers.google.com/apis/api/places_backend?project = _“,”html_attributions“:[],”results“:[],”status“:”REQUEST_DENIED“}

我该如何解决?

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

API_KEY

1 回答

  • 1

    我认为你的api密钥不匹配

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

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

    enter image description here

    • 如果你想从浏览器测试你的api,你不应该对api key有任何限制..在限制中选择none .

    • 还使用此链接测试您附近的地方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());
    

相关问题