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

前端之家收集整理的这篇文章主要介绍了使用Android中的Google Maps API从用户位置获取附近地点的结果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是第一次使用谷歌地图API和谷歌地方API.我正在做一个演示应用程序,显示最靠近用户位置的列表医院(例如)以及到每个医院的路线.我能够通过以下代码获取用户的位置:
  1. public class MainActivity extends ActionBarActivity {
  2. private GoogleMap map;
  3. UiSettings mapSettings;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9.  
  10. map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
  11.  
  12. if (map != null) {
  13. // map.addMarker(new MarkerOptions().position(myLocation).title("Start"));
  14. map.setMyLocationEnabled(true);
  15. mapSettings = map.getUiSettings();
  16. mapSettings.setScrollGesturesEnabled(true);
  17. mapSettings.setZoomControlsEnabled(true);
  18. }
  19. }
  20. }

xml文件

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
  9.  
  10. <fragment
  11. android:id="@+id/map"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. class="com.google.android.gms.maps.MapFragment" />
  15.  
  16.  
  17. </RelativeLayout>

我的问题如何使用google places API获取医院列表并显示它们.任何帮助,提示,演练或教程都将被认可.谢谢你提前.

解决方法

对于Android,我尝试使用Android的Place picker.它可以将内置的位置选择器UI小部件添加到您的应用程序,以便用户可以从地图上显示的一组附近位置中进行选择.它易于使用,您只需要做的是:
  1. int PLACE_PICKER_REQUEST = 1;
  2. PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
  3. startActivityForResult(builder.build(this),PLACE_PICKER_REQUEST);

它将显示下面的UI为您选择一个地方,您可以通过以下方式获取地点信息:

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

有关详细信息,请参阅指南here代码here.

但是,它无法设置场所类型,因为它没有Build in UI的功能.如果您确实需要设置,则应使用Google Places API Web服务API here,自行搜索位置,解析JSON数据并显示你自己的UI.

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

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

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

请注意,在参数中您需要设置types = hospital

编辑

有关如何使用google places API Web API Web服务API在JSON中传递特定位置的搜索请求的示例.

请求

  1. location: -33.8670,151.1957
  2. radius: 500
  3. types: food
  4. name: cruise
  5. 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像这样:

  1. {
  2. "debug_log" : {
  3. "line" : []
  4. },"html_attributions" : [],"logging_info" : {
  5. "experiment_id" : [],"query_geographic_location" : "AU"
  6. },"results" : [
  7. {
  8. "geometry" : {
  9. "location" : {
  10. "lat" : -33.86879,"lng" : 151.194217
  11. }
  12. },"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png","id" : "21a0b251c9b8392186142c798263e289fe45b4aa","name" : "Rhythmboat Cruises","opening_hours" : {
  13. "open_now" : false,"weekday_text" : []
  14. },"photos" : [
  15. {
  16. "height" : 426,"html_attributions" : [
  17. "Rhythmboat Cruises"
  18. ],"photo_reference" : "CmRdAAAA-YL_I_Gk02blOX6S0nKHry8PFu9pDyp3Y9AnqISsa3Eq8mkbdD5mXuu1Fax60s0nSy3iiX-h5j-ztyLHcc1-782MsLQsgLLa4t3ZgDmCMll-a8ABapZGnZwDKByk67LFEhBgedv_u_eYFSEO9ay8jxJjGhTUHKPJ4G82vBJqSNliuv7UlAtclw","width" : 640
  19. }
  20. ],"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"
  21. },{
  22. "geometry" : {
  23. "location" : {
  24. "lat" : -33.867591,"lng" : 151.201196
  25. }
  26. },"id" : "a97f9fb468bcd26b68a23072a55af82d4b325e0d","name" : "Australian Cruise Group","photos" : [
  27. {
  28. "height" : 1331,"html_attributions" : [
  29. "Australian Cruise Group"
  30. ],"photo_reference" : "CmRdAAAAtEoj29FJcNBccrsu6bHt0xgwVGhYlciCY1fe6gTr_d5_KkeP3LITnOwnpNRJWnX39B04-aIBOKXKJH6ltx948T5vWIYBoah1yZDXsWngWZ5kMsK7xyCB5P_q_xBIBxxUEhAYWeB4PiOm_Jy093fB-j0iGhSYmIs9xB2aa6u-RH8V8lZEk-Q5ig","width" : 2000
  31. }
  32. ],"place_id" : "ChIJrTLr-GyuEmsRBfy61i59si0","reference" : "CnRqAAAAFbukrZvRNsc05TreHUCrPEya5NcN9v0fFLLaK-D1fSyxFTuQlUDhDstU3qwXKw_fADX4W6guUkexax1nufgiYIuGCKoZPEnup1r-LhGGNz9dn1uf9Of5iOtZ1XgCeDjJaYvGbSB3C0pAXL8r9kOsmhIQbx2Sia2DAWTjtSZwuh5aehoUhM6upqBCDLhGruZAGGsCOwAiIq8","types" : [
  33. "restaurant","travel_agency","establishment"
  34. ],"vicinity" : "32 The Promenade,King Street Wharf 5,Sydney"
  35. },{
  36. "geometry" : {
  37. "location" : {
  38. "lat" : -33.870943,"lng" : 151.190311
  39. }
  40. },"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png","id" : "e644f7f34cf875b9919c6548f1b721947362850a","name" : "Lunch Cruise with Jazz on Sydney Harbour","photos" : [
  41. {
  42. "height" : 292,"html_attributions" : [
  43. "From a Google User"
  44. ],"photo_reference" : "CmRdAAAAR4bqFTKYWdBwZwdBdON_JRD7V_joTwwIPwRUpZIZWkSSd8GQ3P2O-_aQbUJdL2RhoAyzCUIF0f--DI4oXFneTpj5zZfFq-iFiT7i_x0tjnDveIY8tJv-6o0uWSSjYqabEhCKqQWZqrKAoddjDcc64N48GhQZ2T1_ntPzNKCooHpZzlYQ7AxFOA","width" : 438
  45. }
  46. ],"place_id" : "ChIJLfySpTOuEmsRPCRKrzl8ZEY","reference" : "CoQBewAAAFd2fO_YWGTiT4RzXWb5tsOuOt7YyV_ScQOwm0tqJSrAyACCczeOzV-P_mgZLro1oKP_34Nt0nVC_1OEKAQUcd7cUm7xmAMSX-EkbSWiD0kOWGgGgKuDRtb0t_8qsxBGU_izugWCyK7SRWezTxELYNdkS0OEiSWPnvhxvXuQktBBEhAAtEe7fagW2kUR14T1QpVsGhQBO7YpIyYSPvo4zUJuL_bX30nJZw","types" : [ "bar","restaurant","vicinity" : "37 Bank St,Pyrmont"
  47. }
  48. ],"status" : "OK"
  49. }

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

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

有关详细信息,请参阅here.

猜你在找的Android相关文章