android - 实现 Android Google map 搜索功能 V2

标签 android google-places-api android-maps-v2

enter image description here

我想在我的 Android map 应用程序中实现对国家/州/城市或附近地点的搜索,我知道这个问题已经被问过 here但我想再次提出它,因为我仍然在努力寻找任何优雅的解决方案。我还提到了 this帖子但读了一些评论后我失去了希望。所以如果有人在android中实现了places api?

最佳答案

最简单的方法是使用 Google Geocoding API。您可以使用

访问它

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA

这将返回一个 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请求并接收回JSONObjectreturnval 是我声明的一个JSONObjecturl 是上面给出的 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/

相关文章:

Android 谷歌地图标记禁用导航选项

android - 如何找出应用程序的安装位置?

android - 如何在 Android 的 RecyclerView 中添加 LinearLayout 管理器和 GridLayout 管理器

java - android实现不同api级别的方法

ios - 有没有像 google Place Search API 这样的 Apple API?

ios - 后台判断用户是否在特定位置

java - 从数组中删除一个单词

objective-c - 在同一个应用程序中同时使用 Facebook API 和 Google Places API 的 JSON 库问题

android - 给定设备位置和半径生成随机 LatLng

放大时带有插图概览的 Android 谷歌地图