android - 在 Android 中使用 Google Maps API 从用户位置获取附近地点的结果

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

这是第一次使用 google map API 和 google places API。我正在做一个演示应用程序,显示距离用户位置最近的列表医院(例如)以及到每家医院的路线。我能够使用以下代码获取用户的位置:

public class MainActivity extends ActionBarActivity {
    private GoogleMap map;
    UiSettings mapSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        if (map != null) {
           // map.addMarker(new MarkerOptions().position(myLocation).title("Start"));
            map.setMyLocationEnabled(true);
            mapSettings = map.getUiSettings();
            mapSettings.setScrollGesturesEnabled(true);
            mapSettings.setZoomControlsEnabled(true);
        }
    }
}

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />


</RelativeLayout>

我的问题是如何使用 google places API 获取医院列表并显示它们。任何帮助、提示、演练或教程都将真正适用。提前致谢。

最佳答案

对于 Android,我尝试使用 Place picker for Android。它可以将内置地点选择器 UI 小部件 添加到您的应用程序,因此用户可以从 map 上显示的一组附近地点中进行选择。它简单易用,您只需要做的是:

int PLACE_PICKER_REQUEST = 1;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();    
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);

它会显示下面的 UI 来为您选择一个地点,您可以使用以下方法获取地点信息:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == PLACE_PICKER_REQUEST) {
    if (resultCode == RESULT_OK) {
        Place place = PlacePicker.getPlace(data, this);
        String toastMsg = String.format("Place: %s", place.getName());
        Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
    }
  }
}

enter image description here

更多详情请引用指南here和代码 here .

但是,它不能设置地点类型,因为它没有 Build in UI 的功能。如果你真的需要设置,你应该使用 Google Places API Web Service API here ,自己搜索位置,解析JSON数据,展示在自己的UI上。

“附近搜索”可让您搜索指定区域内的地点。您可以通过提供关键字或指定要搜索的地点类型来优化搜索请求。

附近搜索请求是以下形式的 HTTP URL:

https://maps.googleapis.com/maps/api/place/nearbysearch/output?parameters

注意在参数中你需要设置types=hospital

编辑

有关如何使用 google places API web API 网络服务 API 传递对 JSON 中特定位置的搜索请求的示例。

请求

location: -33.8670,151.1957
radius: 500
types: food
name: cruise
key: API_KEY

此网址:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670,151.1957&radius=500&types=food&name=cruise&key=API_KEY

和像这样的响应 JSON:

{
   "debug_log" : {
      "line" : []
   },
   "html_attributions" : [],
   "logging_info" : {
      "experiment_id" : [],
      "query_geographic_location" : "AU"
   },
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : -33.86879,
               "lng" : 151.194217
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "21a0b251c9b8392186142c798263e289fe45b4aa",
         "name" : "Rhythmboat Cruises",
         "opening_hours" : {
            "open_now" : false,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 426,
               "html_attributions" : [
                  "\u003ca href=\"https://www.google.com/maps/views/profile/104066891898402903288\"\u003eRhythmboat Cruises\u003c/a\u003e"
               ],
               "photo_reference" : "CmRdAAAA-YL_I_Gk02blOX6S0nKHry8PFu9pDyp3Y9AnqISsa3Eq8mkbdD5mXuu1Fax60s0nSy3iiX-h5j-ztyLHcc1-782MsLQsgLLa4t3ZgDmCMll-a8ABapZGnZwDKByk67LFEhBgedv_u_eYFsEo9ay8jxJjGhTUHKPJ4G82vBJqSNliuv7UlAtclw",
               "width" : 640
            }
         ],
         "place_id" : "ChIJyWEHuEmuEmsRm9hTkapTCrk",
         "reference" : "CnRmAAAAvQlMKw-XtxEY4vWFCvudF7CEMQGI5ycNbfVgGl9rAF75fdiPOiLJw1k9NL2v8ZIJsOJuRS3Lm9Dw1vga4ajycAs7PlxN1MVnnYT9la0pBvEvSQNlyvszKANS1R4P7Mvk_jhqswMggqCUtwJ13LN2hRIQOiAkLTWUi3DOjVVOw7J5IRoUb_cJyJaJNqKdmkDM2f0OjQjh9F0",
         "scope" : "GOOGLE",
         "types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
         "vicinity" : "Pyrmont Bay Wharf (Near Australia Maritime Museum), Pyrmont, NSW 2009"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.867591,
               "lng" : 151.201196
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "a97f9fb468bcd26b68a23072a55af82d4b325e0d",
         "name" : "Australian Cruise Group",
         "opening_hours" : {
            "open_now" : false,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 1331,
               "html_attributions" : [
                  "\u003ca href=\"https://www.google.com/maps/views/profile/110751364053842618118\"\u003eAustralian Cruise Group\u003c/a\u003e"
               ],
               "photo_reference" : "CmRdAAAAtEoj29FJcNBccrsu6bHt0xgwVGhYlciCY1fe6gTr_d5_KkeP3LITnOwnpNRJWnX39B04-aIBOKXKJH6ltx948T5vWIYBoah1yZDXsWngWZ5kMsK7xyCB5P_q_xBIBxxUEhAYWeB4PiOm_Jy093fB-j0iGhSYmIs9xB2aa6u-RH8V8lZEk-Q5ig",
               "width" : 2000
            }
         ],
         "place_id" : "ChIJrTLr-GyuEmsRBfy61i59si0",
         "reference" : "CnRqAAAAFbukrZvRNsc05TreHUCrPEya5NcN9v0fFLLaK-D1fSyxFTuQlUDhDstU3qwXKw_fADX4W6guUkexax1nufgiYIuGCKoZPEnup1r-LhGGNz9dn1uf9Of5iOtZ1XgCeDjJaYvGbSB3C0pAXL8r9kOsmhIQbx2Sia2DAWTjtSZwuh5aehoUhM6upqBCDLhGruZAGGsCOwAiIq8",
         "scope" : "GOOGLE",
         "types" : [
            "restaurant",
            "travel_agency",
            "food",
            "point_of_interest",
            "establishment"
         ],
         "vicinity" : "32 The Promenade, King Street Wharf 5, Sydney"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.870943,
               "lng" : 151.190311
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png",
         "id" : "e644f7f34cf875b9919c6548f1b721947362850a",
         "name" : "Lunch Cruise with Jazz on Sydney Harbour",
         "opening_hours" : {
            "open_now" : false,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 292,
               "html_attributions" : [
                  "\u003ca href=\"https://www.google.com/maps/views/profile/105423912060796272053\"\u003eFrom a Google User\u003c/a\u003e"
               ],
               "photo_reference" : "CmRdAAAAR4bqFTKYWdBwZwdBdON_JRD7V_joTwwIPwRUpZIZWkSSd8GQ3P2O-_aQbUJdL2RhoAyzCUIF0f--DI4oXFneTpj5zZfFq-iFiT7i_x0tjnDveIY8tJv-6o0uWSSjYqabEhCKqQWZqrKAoddjDcc64N48GhQZ2T1_ntPzNKCooHpZzlYQ7AxFOA",
               "width" : 438
            }
         ],
         "place_id" : "ChIJLfySpTOuEmsRPCRKrzl8ZEY",
         "reference" : "CoQBewAAAFd2fO_YWGTiT4RzXWb5tsOuOt7YyV_ScQOwm0tqJSrAyACCczeOzV-P_mgZLro1oKP_34Nt0nVC_1OEKAQUcd7cUm7xmAMSX-EkbSWiD0kOWGgGgKuDRtb0t_8qsxBGU_izugWCyK7SRWezTxELYNdkS0OEiSWPnvhxvXuQktBBEhAAtEe7fagW2kUR14T1QpVsGhQBO7YpIyYSPvo4zUJuL_bX30nJZw",
         "scope" : "GOOGLE",
         "types" : [ "bar", "restaurant", "food", "point_of_interest", "establishment" ],
         "vicinity" : "37 Bank St, Pyrmont"
      }
   ],
   "status" : "OK"
}

以下示例返回英国伦敦附近的医院列表。

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=51.503186,-0.126446&radius=5000&types=hospital&key=API_KEY

更多详情请引用here .

关于android - 在 Android 中使用 Google Maps API 从用户位置获取附近地点的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32393134/

相关文章:

android - Flutter 构建错误 : AAPT: error: resource android:attr/fontVariationSettings, ttcIndex 未找到

javascript - 在 Angular 谷歌地图中显示集群中的标记数量

android - WMS On Android 磁贴加载问题

android - 根据 LatLngBounds 的 GoogleMap moveCamera 或 animateCamera 不准确

android - Google Maps API v2 在 MapFragment 上绘制部分圆圈

java - 每次打开 Android 虚拟设备时,Android SQLite 数据库都不会保留它的值

android - android mvvm大量的观察者

javascript - 自定义街景 map 上的响应式叠加 div

android - "Google Play services are updating"发布时出错,不是模拟器(Google Play Services 12.6.73,华为设备)

设备上的 Android MediaPlayer 不同