Android Baidu map API poi

Source: Internet
Author: User

Previously, only the positioning function and the function of searching the surrounding poi were implemented, so such a program can only be a demo of the toys program.
The actual purpose should be to locate the location of my current device and search for poi around me based on the location.
Although I have provided two complete examples in theory, but in actual implementation, I found that the positioning location latitude and longitude (or parameters) cannot be uploaded to my search module.
So Tangle! But now I know what's going on.

1) Layout file Res/layout/Main. xml

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Android: Orientation = "vertical">
  6. <Textview
  7. Android: layout_width = "fill_parent"
  8. Android: layout_height = "wrap_content"
  9. Android: text = "@ string/Hello"/>
  10. <Com. Baidu. mapapi. mapview
  11. Android: Id = "@ + ID/map_view"
  12. Android: layout_width = "fill_parent"
  13. Android: layout_height = "fill_parent"
  14. Android: clickable = "true"/>
  15. </Linearlayout>

Copy code

The layout file is basically in this format. You can add some other functions here.
2) inherits the activity class of COM. Baidu. mapapi. mapactivity.

  1. Package com. Search. Map;
  2. Import Android. Location. location;
  3. Import Android. OS. Bundle;
  4. Import com. Baidu. mapapi. bmapmanager;
  5. Import com. Baidu. mapapi. geopoint;
  6. Import com. Baidu. mapapi. locationlistener;
  7. Import com. Baidu. mapapi. mkaddrinfo;
  8. Import com. Baidu. mapapi. mkdrivingrouteresult;
  9. Import com. Baidu. mapapi. mkpoiresult;
  10. Import com. Baidu. mapapi. mksearch;
  11. Import com. Baidu. mapapi. mksearchlistener;
  12. Import com. Baidu. mapapi. mktransitrouteresult;
  13. Import com. Baidu. mapapi. mkwalkingrouteresult;
  14. Import com. Baidu. mapapi. mapactivity;
  15. Import com. Baidu. mapapi. mapcontroller;
  16. Import com. Baidu. mapapi. mapview;
  17. Import com. Baidu. mapapi. mylocationoverlay;
  18. Import com. Baidu. mapapi. poioverlay;
  19. Public class extends earchactivity extends mapactivity {
  20. // Define the map engine management class
  21. Private bmapmanager mapmanager; // defines the search service class
  22. Private mksearch mmksearch;
  23. Private mapview;
  24. Private mapcontroller;
  25. Locationlistener mlocationlistener = NULL; // This listener is registered when onresume is used, and remove is required when onpause is used.
  26. Mylocationoverlay mlocationoverlay = NULL; // locate the Layer
  27. @ Override
  28. Public void oncreate (bundle savedinstancestate ){
  29. Super. oncreate (savedinstancestate );
  30. Setcontentview (R. layout. Main );
  31. // Initialize mapactivity
  32. Mapmanager = new bmapmanager (getapplication ());
  33. // Set the first parameter of the init Method to the applied apikey.
  34. Mapmanager. INIT ("285b450ebab2a92293e85502150ada7f03c777c4", null );
  35. Super. initmapactivity (mapmanager );
  36. Mapview = (mapview) findviewbyid (R. Id. map_view );
  37. // Set map mode to traffic map
  38. Mapview. settraffic (true );
  39. // Set to enable the built-in zoom control
  40. Mapview. setbuiltinzoomcontrols (true );
  41. // Set overlay to be displayed during the scaling animation. The default setting is not to be drawn.
  42. Mapview. setdrawoverlaywhenzooming (true );
  43. // Add a positioning Layer
  44. Mlocationoverlay = new mylocationoverlay (this, mapview );
  45. Mapview. getoverlays (). Add (mlocationoverlay );
  46. // Register and locate the event
  47. Mlocationlistener = new locationlistener (){
  48. @ Override
  49. Public void onlocationchanged (location ){
  50. If (location! = NULL ){
  51. Geopoint = new geopoint (INT) (location. getlatitude () * 1e6 ),
  52. (INT) (location. getlongpolling () * 1e6 ));
  53. Mapview. getcontroller (). animateto (geopoint );
  54. Mapcontroller = mapview. getcontroller ();
  55. // Set the map center
  56. Mapcontroller. setcenter (geopoint );
  57. // Set the default zoom level of the map
  58. Mapcontroller. setzoom (16 );
  59. // Initialization
  60. Mksearch mmksearch = new mksearch ();
  61. Mmksearch. INIT (mapmanager, new mysearchlistener ());
  62. // Search for ATMs within 500 meters near the entrance of Guizhou University
  63. Mmksearch. receivearchnearby ("ATM", geopoint, 500 );
  64. }
  65. }
  66. };
  67. }
  68. @ Override
  69. Protected Boolean isroutedisplayed (){
  70. Return false;
  71. }
  72. @ Override
  73. Protected void ondestroy (){
  74. If (mapmanager! = NULL ){
  75. // Call this method before exiting the program
  76. Mapmanager. Destroy ();
  77. Mapmanager = NULL;
  78. }
  79. Super. ondestroy ();
  80. }
  81. @ Override
  82. Protected void onpause (){
  83. If (mapmanager! = NULL ){
  84. // Terminate Baidu map API
  85. Mapmanager. getlocationmanager (). removeupdates (mlocationlistener );
  86. Mlocationoverlay. disablemylocation ();
  87. Mlocationoverlay. disablecompass (); // close the compass
  88. Mapmanager. Stop ();
  89. }
  90. Super. onpause ();
  91. }
  92. @ Override
  93. Protected void onresume (){
  94. If (mapmanager! = NULL ){
  95. // Enable Baidu map API
  96. // Register the positioning event and move the map to the positioning point
  97. Mapmanager. getlocationmanager (). requestlocationupdates (mlocationlistener );
  98. Mlocationoverlay. enablemylocation ();
  99. Mlocationoverlay. enablecompass (); // enable the compass
  100. Mapmanager. Start ();
  101. }
  102. Super. onresume ();
  103. }
  104. /**
  105. ** Implement the mksearchlistener interface to implement the asynchronous Search Service * @ author Liufeng
  106. */
  107. Public class mysearchlistener implements mksearchlistener {
  108. /*** Search address information result based on latitude and longitude ** @ Param result search result * @ Param ierror error number (0 indicates correct return )*/
  109. @ Override
  110. Public void ongetaddrresult (mkaddrinfo result, int ierror ){
  111. }
  112. /*** Driving route search result ** @ Param result search result * @ Param ierror error code */
  113. @ Override
  114. Public void ongetdrivingrouteresult (mkdrivingrouteresult result, int ierror ){
  115. }
  116. /**
  117. ** Poi search result (range search, city poi search, and peripheral search) ** @ Param result search result * @ Param type
  118. * Type of the returned result (, 21: poi list 7: City list) * @ Param ierror error code (0 indicates that the returned result is correct)
  119. */
  120. @ Override
  121. Public void ongetpoiresult (mkpoiresult result, int type, int ierror ){
  122. If (result = NULL ){
  123. Return;
  124. }
  125. // Poioverlay is an overlay provided by the Baidu map API for displaying poi
  126. Poioverlay = new poioverlay (poisearchactivity. This, mapview );
  127. // Set the searched poi data
  128. Poioverlay. setdata (result. getallpoi ());
  129. // Display poioverlay on the map (add the points of interest to the map)
  130. Mapview. getoverlays (). Add (poioverlay );
  131. }
  132. /*** Bus transfer Route Search Result ** @ Param result search result * @ Param ierror error code (0 indicates correct return )*/
  133. @ Override
  134. Public void ongettransitrouteresult (mktransitrouteresult result, int ierror ){
  135. }
  136. /*** Walking Route Search Result ** @ Param result search result * @ Param ierror error code (0 indicates correct return )*/
  137. @ Override
  138. Public void ongetwalkingrouteresult (mkwalkingrouteresult result, int ierror ){
  139. }
  140. }
  141. }

Copy code

3) configuration in androidmanifest. xml

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Package = "com. Search. Map"
  4. Android: versioncode = "1"
  5. Android: versionname = "1.0" type = "codeph" text = "/codeph">
  6. <Uses-SDK Android: minsdkversion = "8"/>
  7. <Application
  8. Android: icon = "@ drawable/ic_launcher"
  9. Android: Label = "@ string/app_name">
  10. <Activity
  11. Android: Label = "@ string/app_name"
  12. Android: Name = "earearchactivity">
  13. <Intent-filter>
  14. <Action Android: Name = "android. Intent. Action. Main"/>
  15. <Category Android: Name = "android. Intent. Category. launcher"/>
  16. </Intent-filter>
  17. </Activity>
  18. </Application>
  19. <! -- Access Network permissions -->
  20. <Uses-Permission Android: Name = "android. Permission. Internet"/>
  21. <! -- Permission for precise access -->
  22. <Uses-Permission Android: Name = "android. Permission. access_fine_location"/>
  23. <! -- Access network status permissions -->
  24. <Uses-Permission Android: Name = "android. Permission. access_network_state"/>
  25. <! -- Access the WiFi network status -->
  26. <Uses-Permission Android: Name = "android. Permission. access_wifi_state"/>
  27. <! -- Change the permission of the Wi-Fi network -->
  28. <Uses-Permission Android: Name = "android. Permission. change_wifi_state"/>
  29. <! -- Read and write permissions on the memory card -->
  30. <Uses-Permission Android: Name = "android. Permission. write_external_storage"/>
  31. <! -- Permission to read the phone status -->
  32. <Uses-Permission Android: Name = "android. Permission. read_phone_state"/>
  33. </Manifest>

Copy code

In this process, pay special attention to the fact that the simulator cannot be located. When you add a positioning module, all outgoing parameters are empty.
This method function is implemented through the callback interface. When this event is triggered, the longitude and latitude positions must be changed.
The official documentation is clearly written. Simply put, you cannot implement GPS Positioning without a device (only real-machine testing is supported). GPS positioning is real-time monitoring and positioning, you are receiving and transmitting your location information at any time. Only constant changes can trigger this location event.
(I have to say that Baidu developers are very careful)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.