无法找到附近的地方
Unable to find nearby places
我想找附近的医院。我的代码工作正常,但没有显示任何 toast 消息或附近的位置。
它只显示我当前的位置,不显示我附近的位置。这是因为 google 地方关键吗?
MainActivity.kt
private String getUrl(double latitude , double longitude , String nearbyPlace){
StringBuilder googlePlaceUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlaceUrl.append("location="+latitude+","+longitude);
googlePlaceUrl.append("&radius="+PROXIMITY_RADIUS);
googlePlaceUrl.append("&type="+nearbyPlace);
googlePlaceUrl.append("&sensor=true");
googlePlaceUrl.append("&key="+"AIzaSyCYjKCCqaFeeCO6zqfuRVudrV5FzrCTkZk");
//
googlePlaceUrl.append("&key="+"AIzaSyAHbKWGMnqv0GJhaVj1XKMXFZELdzzXK");
Log.d("MapsActivity", "url = "+googlePlaceUrl.toString());
return googlePlaceUrl.toString();
}
NearbyPlacesData.kt
override fun onPostExecute(result: String) {
var nearbyPlaceList: List<HashMap<String,String>>
var parser = DataParser()
nearbyPlaceList = parser.parse(result)
showNearbyPlaces(nearbyPlaceList)
Toast.makeText(context,result,Toast.LENGTH_SHORT).show()
}
private fun showNearbyPlaces(nearbyPlaceList: List<HashMap<String,String>>){
for (i in 0 until nearbyPlaceList.size){
var markerOptions = MarkerOptions()
var googlePlace = nearbyPlaceList.get(i)
var placeName = googlePlace.get("place_name")
var vicinity = googlePlace.get("vicinity")
var lat = java.lang.Double.parseDouble(googlePlace.get("lat"))
var lng = java.lang.Double.parseDouble(googlePlace.get("lng"))
var latLng = LatLng(lat,lng)
markerOptions.position(latLng)
markerOptions.title(placeName + " : " + vicinity)
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))
mMap!!.addMarker(markerOptions)
mMap!!.moveCamera(CameraUpdateFactory.newLatLng(latLng))
mMap!!.animateCamera(CameraUpdateFactory.zoomTo(10f))
}
我解决了这个问题。我在使用 PLACES API 时没有激活它。激活 PLACES API 后,代码运行正常。
谢谢大家
我想找附近的医院。我的代码工作正常,但没有显示任何 toast 消息或附近的位置。 它只显示我当前的位置,不显示我附近的位置。这是因为 google 地方关键吗?
MainActivity.kt
private String getUrl(double latitude , double longitude , String nearbyPlace){
StringBuilder googlePlaceUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlaceUrl.append("location="+latitude+","+longitude);
googlePlaceUrl.append("&radius="+PROXIMITY_RADIUS);
googlePlaceUrl.append("&type="+nearbyPlace);
googlePlaceUrl.append("&sensor=true");
googlePlaceUrl.append("&key="+"AIzaSyCYjKCCqaFeeCO6zqfuRVudrV5FzrCTkZk");
//
googlePlaceUrl.append("&key="+"AIzaSyAHbKWGMnqv0GJhaVj1XKMXFZELdzzXK");
Log.d("MapsActivity", "url = "+googlePlaceUrl.toString());
return googlePlaceUrl.toString();
}
NearbyPlacesData.kt
override fun onPostExecute(result: String) {
var nearbyPlaceList: List<HashMap<String,String>>
var parser = DataParser()
nearbyPlaceList = parser.parse(result)
showNearbyPlaces(nearbyPlaceList)
Toast.makeText(context,result,Toast.LENGTH_SHORT).show()
}
private fun showNearbyPlaces(nearbyPlaceList: List<HashMap<String,String>>){
for (i in 0 until nearbyPlaceList.size){
var markerOptions = MarkerOptions()
var googlePlace = nearbyPlaceList.get(i)
var placeName = googlePlace.get("place_name")
var vicinity = googlePlace.get("vicinity")
var lat = java.lang.Double.parseDouble(googlePlace.get("lat"))
var lng = java.lang.Double.parseDouble(googlePlace.get("lng"))
var latLng = LatLng(lat,lng)
markerOptions.position(latLng)
markerOptions.title(placeName + " : " + vicinity)
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))
mMap!!.addMarker(markerOptions)
mMap!!.moveCamera(CameraUpdateFactory.newLatLng(latLng))
mMap!!.animateCamera(CameraUpdateFactory.zoomTo(10f))
}
我解决了这个问题。我在使用 PLACES API 时没有激活它。激活 PLACES API 后,代码运行正常。 谢谢大家