大家早上好。 我正在尝试实施一项活动来查找用户附近的某些特定地点。
我尝试了这段代码:MapActivity query for nearest hospital/restaurant not working
现在进行一些更改后,地图正在运行并显示用户的当前位置,但不显示地点。
我已激活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" :[], "结果" :[], "状态" :" REQUEST_DENIED" }
我该如何解决?
编辑:我的控制台是这样的:答案 0 :(得分:1)
我认为你的api密钥不匹配
看到你的截图后......你应该做几件事:
Google Maps APIs
下如果你想从浏览器测试你的api,你不应该对api密钥施加任何限制..在限制中选择无..
还可以使用此链接测试您附近的地方api:我认为您的网址中缺少某些内容
以下是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());