使用地图查找特定的附近地点
Posted
技术标签:
【中文标题】使用地图查找特定的附近地点【英文标题】:Using maps to find specific nearby places 【发布时间】:2017-05-12 19:42:36 【问题描述】:大家早上好。 我正在尝试实施一项活动来查找用户附近的一些特定地点。
我试过这个代码:MapActivity query for nearest hospital/restaurant not working
经过一些更改,现在地图可以正常工作并显示用户的当前位置,而不是地点。
我已激活 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" : "此 API 项目无权使用此 API。请确保在 Google Developers Console 中激活此 API:https://console.developers.google.com/apis/api/places_backend?project=_", “html_attributions”:[], “结果” : [], “状态”:“REQUEST_DENIED”
我该如何解决?
编辑:我的控制台是这样的:
【问题讨论】:
【参考方案1】:我认为你的 api 密钥不匹配
看到你的截图后......你应该做几件事:
-
首先,您应该在开发者控制台中为 Web 服务启用 Google place api。它在
Google Maps APIs
下列出
如果你想从浏览器测试你的 api,你不应该对 api 键施加任何限制..在限制中选择 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);
最后用方法as..
StringBuilder sbValue = new StringBuilder(sbMethod());
PlacesTask placesTask = new PlacesTask();
placesTask.execute(sbValue.toString());
【讨论】:
我会试试的。感谢您的帮助。 我在控制台收到了同样的消息,应用仍然只显示用户的当前位置。 在浏览器上* 有一个疑问,@rafsanahmad007,SHA1 来自用于导出 apk 的密钥库还是来自 debug.keystore? 如果您想发布您的应用程序,那么您可以使用:SHA1 来自用于导出 apk 的密钥库...但如果它处于开发阶段,您应该使用调试 sha1...来自您的安卓工作室以上是关于使用地图查找特定的附近地点的主要内容,如果未能解决你的问题,请参考以下文章
形成 Google Maps URL,用于搜索特定坐标附近的特定地点