我想在我的 Android map 应用程序中实现对国家/州/城市或附近地点的搜索,我知道这个问题已经被问过 here但我想再次提出它,因为我仍然在努力寻找任何优雅的解决方案。我还提到了 this帖子但读了一些评论后我失去了希望。所以如果有人在android中实现了places api?
最佳答案
最简单的方法是使用 Google Geocoding API
。您可以使用
这将返回一个 JSON
,其中包含该地址的位置数据,包括纬度和经度。以下是我所做的一些代码 fragment :
try{
JSONConnectorGet jget=new JSONConnectorGet(url);
returnval=jget.connectClient();
results=returnval.getJSONArray("results");
resultobj=results.getJSONObject(0);
System.out.println(resultobj.toString());
geometry=resultobj.getJSONObject("geometry");
location=geometry.getJSONObject("location");
lati=location.getDouble("lat");
longi=location.getDouble("lng");
}
catch(Exception e){
e.printStackTrace();
}
a=new LatLng(lati, longi);
这里JSONConnectorGet
是我编写的一个类,可用于发送HttpGet
请求并接收回JSONObject
。 returnval
是我声明的一个JSONObject
。 url
是上面给出的 URL,其中地址替换为搜索到的内容。
然后您必须从 JSONObject
中提取 LatLng
。我使用的 JSONObjects 和 JSONArrays 是在 try-catch block 之外声明的。现在我在 a
中获得了搜索地点的纬度和经度。
现在我只需将相机移动到该位置即可。
eventmap.animateCamera(CameraUpdateFactory.newLatLngZoom(a, 3.0f));
如果需要,您也可以在该 LatLng
处添加标记。
关于android - 实现 Android Google map 搜索功能 V2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24055610/