Java GooglePlacesAPI:如何从纬度和经度获取照片和地点id?
我想获取并放置我当前位置的Google place API所需的\u id 在附近搜索不会返回我的确切位置。(安卓定位服务返回的当前lat/lng) 雷达搜索需要一个关键词。请建议。根据文档,您需要提供的三件事是钥匙、位置和半径。我删掉了一堆不必要的代码,但我是这样做的 1) 获取当前位置Java GooglePlacesAPI:如何从纬度和经度获取照片和地点id?,java,android,google-places-api,google-places,Java,Android,Google Places Api,Google Places,我想获取并放置我当前位置的Google place API所需的\u id 在附近搜索不会返回我的确切位置。(安卓定位服务返回的当前lat/lng) 雷达搜索需要一个关键词。请建议。根据文档,您需要提供的三件事是钥匙、位置和半径。我删掉了一堆不必要的代码,但我是这样做的 1) 获取当前位置 private void initializeMapLocation() { LocationManager locationManager = (LocationManager) this
private void initializeMapLocation() {
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
Location lastLocation = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastLocation != null) {
setUserLocation(lastLocation);
}
}
private void setUserLocation(Location location) {
LatLng currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLng(currentLatLng));
}
2) 建立你的搜索网址。如果需要的话,可以添加额外的参数,比如关键字,但在这种特殊情况下,听起来并不是你想要的
private void buildAndInitiateSearchTask(String searchType) {
Projection mProjection = mMap.getProjection();
LatLng mProjectionCenter = mProjection.getVisibleRegion().latLngBounds
.getCenter();
searchURL.append("https://maps.googleapis.com/maps/api/place/nearbysearch/");
searchURL.append("json?");
searchURL.append("location=" + mProjectionCenter.latitude + "," + mProjectionCenter.longitude);
searchURL.append("&radius=" + calculateProjectionRadiusInMeters(mProjection));
searchURL.append("&key=YOUR_KEY_HERE");
new PlaceSearchAPITask().execute(searchURL.toString());
}
private double calculateProjectionRadiusInMeters(Projection projection) {
LatLng farLeft = projection.getVisibleRegion().farLeft;
LatLng nearRight = projection.getVisibleRegion().nearRight;
Location farLeftLocation = new Location("Point A");
farLeftLocation.setLatitude(farLeft.latitude);
farLeftLocation.setLongitude(farLeft.longitude);
Location nearRightLocation = new Location("Point B");
nearRightLocation.setLatitude(nearRight.latitude);
nearRightLocation.setLongitude(nearRight.longitude);
return farLeftLocation.distanceTo(nearRightLocation) / 2 ;
}
3) 发送请求并将结果显示为异步任务
private class PlaceSearchAPITask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... placesURL) {
StringBuilder placesBuilder = new StringBuilder();
for (String placeSearchURL : placesURL) {
HttpClient placesClient = createHttpClient();
try {
HttpGet placesGet = new HttpGet(placeSearchURL);
HttpResponse placesResponse = placesClient
.execute(placesGet);
StatusLine placeSearchStatus = placesResponse
.getStatusLine();
if (placeSearchStatus.getStatusCode() == 200) {
HttpEntity placesEntity = placesResponse
.getEntity();
InputStream placesContent = placesEntity
.getContent();
InputStreamReader placesInput = new InputStreamReader(
placesContent);
BufferedReader placesReader = new BufferedReader(
placesInput);
String lineIn;
while ((lineIn = placesReader.readLine()) != null) {
placesBuilder.append(lineIn);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return placesBuilder.toString();
}
@Override
protected void onPostExecute(String result) {
try {
JSONObject resultObject = new JSONObject(result);
// This is my custom object to hold the pieces of the JSONResult that I want. You would need something else for your particular problem.
mapData = new MapDataSource(resultObject.optJSONArray("results"));
} catch (JSONException e) {
e.printStackTrace();
}
if (mapData != null) {
// TODO - This is where you would add your markers and whatnot.
}
}
}
私有类placeSearchApisTask扩展了AsyncTask{
@凌驾
受保护的字符串背景(字符串…placesURL){
StringBuilder placesBuilder=新StringBuilder();
for(字符串placeSearchURL:placesURL){
HttpClient placesClient=createHttpClient();
试一试{
HttpGet placesGet=新的HttpGet(placeSearchURL);
HttpResponse placesResponse=placesClient
.执行(placesGet);
StatusLine placeSearchStatus=地点响应
.getStatusLine();
if(placeSearchStatus.getStatusCode()==200){
HttpEntity placesEntity=地点响应
.getEntity();
InputStream placesContent=placesEntity
.getContent();
InputStreamReader位置Input=新的InputStreamReader(
地点(内容);
BufferedReader placesReader=新的BufferedReader(
放置量);
弦线;
而((lineIn=placesReader.readLine())!=null){
placesBuilder.append(lineIn);
}
}
}捕获(例外e){
e、 printStackTrace();
}
}
返回placesBuilder.toString();
}
@凌驾
受保护的void onPostExecute(字符串结果){
试一试{
JSONObject resultObject=新的JSONObject(结果);
//这是我的自定义对象,用于保存我想要的JSONResult的各个部分。对于您的特定问题,您可能需要其他东西。
mapData=新的MapDataSource(resultObject.optJSONArray(“结果”);
}捕获(JSONException e){
e、 printStackTrace();
}
if(mapData!=null){
//TODO-这是您添加标记和其他内容的地方。
}
}
}
我们当然可以帮助您,但在此之前,您必须展示您的研究和研发成果!