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
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: Orientation = "vertical">
- <Textview
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: text = "@ string/Hello"/>
- <Com. Baidu. mapapi. mapview
- Android: Id = "@ + ID/map_view"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: clickable = "true"/>
- </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.
- Package com. Search. Map;
- Import Android. Location. location;
- Import Android. OS. Bundle;
- Import com. Baidu. mapapi. bmapmanager;
- Import com. Baidu. mapapi. geopoint;
- Import com. Baidu. mapapi. locationlistener;
- Import com. Baidu. mapapi. mkaddrinfo;
- Import com. Baidu. mapapi. mkdrivingrouteresult;
- Import com. Baidu. mapapi. mkpoiresult;
- Import com. Baidu. mapapi. mksearch;
- Import com. Baidu. mapapi. mksearchlistener;
- Import com. Baidu. mapapi. mktransitrouteresult;
- Import com. Baidu. mapapi. mkwalkingrouteresult;
- Import com. Baidu. mapapi. mapactivity;
- Import com. Baidu. mapapi. mapcontroller;
- Import com. Baidu. mapapi. mapview;
- Import com. Baidu. mapapi. mylocationoverlay;
- Import com. Baidu. mapapi. poioverlay;
- Public class extends earchactivity extends mapactivity {
- // Define the map engine management class
- Private bmapmanager mapmanager; // defines the search service class
- Private mksearch mmksearch;
- Private mapview;
- Private mapcontroller;
- Locationlistener mlocationlistener = NULL; // This listener is registered when onresume is used, and remove is required when onpause is used.
- Mylocationoverlay mlocationoverlay = NULL; // locate the Layer
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- // Initialize mapactivity
- Mapmanager = new bmapmanager (getapplication ());
- // Set the first parameter of the init Method to the applied apikey.
- Mapmanager. INIT ("285b450ebab2a92293e85502150ada7f03c777c4", null );
- Super. initmapactivity (mapmanager );
- Mapview = (mapview) findviewbyid (R. Id. map_view );
- // Set map mode to traffic map
- Mapview. settraffic (true );
- // Set to enable the built-in zoom control
- Mapview. setbuiltinzoomcontrols (true );
- // Set overlay to be displayed during the scaling animation. The default setting is not to be drawn.
- Mapview. setdrawoverlaywhenzooming (true );
- // Add a positioning Layer
- Mlocationoverlay = new mylocationoverlay (this, mapview );
- Mapview. getoverlays (). Add (mlocationoverlay );
- // Register and locate the event
- Mlocationlistener = new locationlistener (){
- @ Override
- Public void onlocationchanged (location ){
- If (location! = NULL ){
- Geopoint = new geopoint (INT) (location. getlatitude () * 1e6 ),
- (INT) (location. getlongpolling () * 1e6 ));
- Mapview. getcontroller (). animateto (geopoint );
- Mapcontroller = mapview. getcontroller ();
- // Set the map center
- Mapcontroller. setcenter (geopoint );
- // Set the default zoom level of the map
- Mapcontroller. setzoom (16 );
- // Initialization
- Mksearch mmksearch = new mksearch ();
- Mmksearch. INIT (mapmanager, new mysearchlistener ());
- // Search for ATMs within 500 meters near the entrance of Guizhou University
- Mmksearch. receivearchnearby ("ATM", geopoint, 500 );
- }
- }
- };
- }
- @ Override
- Protected Boolean isroutedisplayed (){
- Return false;
- }
- @ Override
- Protected void ondestroy (){
- If (mapmanager! = NULL ){
- // Call this method before exiting the program
- Mapmanager. Destroy ();
- Mapmanager = NULL;
- }
- Super. ondestroy ();
- }
- @ Override
- Protected void onpause (){
- If (mapmanager! = NULL ){
- // Terminate Baidu map API
- Mapmanager. getlocationmanager (). removeupdates (mlocationlistener );
- Mlocationoverlay. disablemylocation ();
- Mlocationoverlay. disablecompass (); // close the compass
- Mapmanager. Stop ();
- }
- Super. onpause ();
- }
- @ Override
- Protected void onresume (){
- If (mapmanager! = NULL ){
- // Enable Baidu map API
- // Register the positioning event and move the map to the positioning point
- Mapmanager. getlocationmanager (). requestlocationupdates (mlocationlistener );
- Mlocationoverlay. enablemylocation ();
- Mlocationoverlay. enablecompass (); // enable the compass
- Mapmanager. Start ();
- }
- Super. onresume ();
- }
- /**
- ** Implement the mksearchlistener interface to implement the asynchronous Search Service * @ author Liufeng
- */
- Public class mysearchlistener implements mksearchlistener {
- /*** Search address information result based on latitude and longitude ** @ Param result search result * @ Param ierror error number (0 indicates correct return )*/
- @ Override
- Public void ongetaddrresult (mkaddrinfo result, int ierror ){
- }
- /*** Driving route search result ** @ Param result search result * @ Param ierror error code */
- @ Override
- Public void ongetdrivingrouteresult (mkdrivingrouteresult result, int ierror ){
- }
- /**
- ** Poi search result (range search, city poi search, and peripheral search) ** @ Param result search result * @ Param type
- * Type of the returned result (, 21: poi list 7: City list) * @ Param ierror error code (0 indicates that the returned result is correct)
- */
- @ Override
- Public void ongetpoiresult (mkpoiresult result, int type, int ierror ){
- If (result = NULL ){
- Return;
- }
- // Poioverlay is an overlay provided by the Baidu map API for displaying poi
- Poioverlay = new poioverlay (poisearchactivity. This, mapview );
- // Set the searched poi data
- Poioverlay. setdata (result. getallpoi ());
- // Display poioverlay on the map (add the points of interest to the map)
- Mapview. getoverlays (). Add (poioverlay );
- }
- /*** Bus transfer Route Search Result ** @ Param result search result * @ Param ierror error code (0 indicates correct return )*/
- @ Override
- Public void ongettransitrouteresult (mktransitrouteresult result, int ierror ){
- }
- /*** Walking Route Search Result ** @ Param result search result * @ Param ierror error code (0 indicates correct return )*/
- @ Override
- Public void ongetwalkingrouteresult (mkwalkingrouteresult result, int ierror ){
- }
- }
- }
Copy code
3) configuration in androidmanifest. xml
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
- Package = "com. Search. Map"
- Android: versioncode = "1"
- Android: versionname = "1.0" type = "codeph" text = "/codeph">
- <Uses-SDK Android: minsdkversion = "8"/>
- <Application
- Android: icon = "@ drawable/ic_launcher"
- Android: Label = "@ string/app_name">
- <Activity
- Android: Label = "@ string/app_name"
- Android: Name = "earearchactivity">
- <Intent-filter>
- <Action Android: Name = "android. Intent. Action. Main"/>
- <Category Android: Name = "android. Intent. Category. launcher"/>
- </Intent-filter>
- </Activity>
- </Application>
- <! -- Access Network permissions -->
- <Uses-Permission Android: Name = "android. Permission. Internet"/>
- <! -- Permission for precise access -->
- <Uses-Permission Android: Name = "android. Permission. access_fine_location"/>
- <! -- Access network status permissions -->
- <Uses-Permission Android: Name = "android. Permission. access_network_state"/>
- <! -- Access the WiFi network status -->
- <Uses-Permission Android: Name = "android. Permission. access_wifi_state"/>
- <! -- Change the permission of the Wi-Fi network -->
- <Uses-Permission Android: Name = "android. Permission. change_wifi_state"/>
- <! -- Read and write permissions on the memory card -->
- <Uses-Permission Android: Name = "android. Permission. write_external_storage"/>
- <! -- Permission to read the phone status -->
- <Uses-Permission Android: Name = "android. Permission. read_phone_state"/>
- </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)