Android Baidu map API Integration II "positioning"

Source: Internet
Author: User

Book pick up the back ↑

Basic map Please view Android Baidu map API integration a "basic map": Address http://www.cnblogs.com/dhr125/p/5969980.html

1. declaring the service component in the application tag

<service android:name= "COM.BAIDU.LOCATION.F" android:enabled= "true" android:process= ": Remote" ></service >

2. declaring usage rights

1<!--This permission is used for network positioning--2<uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION" ></uses-permission>3<!--this permission to access GPS location--4<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION" ></uses-permission>5<!--used to access WiFi network information, WiFi information is used for network location--6<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE" ></uses-permission>7<!--get carrier information to support interfaces that provide operator information--8<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></uses-permission>9<!--this permission to get access to WiFi, WiFi information is used for network location--Ten<uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE" ></uses-permission> One<!--to read the current state of the phone-- A<uses-permission android:name= "Android.permission.READ_PHONE_STATE" ></uses-permission> -<!--write extended storage, write data to the expansion card for writing offline location data-- -<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission> the<!--access network, network location needs internet-- -<uses-permission android:name= "Android.permission.INTERNET"/> -<!--SD card read access, user writes offline location data-- -<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS" ></uses-permission>

3. To extract the instance code out to find Locationdemo.java, copy the code as follows

1  PackageCom.example.baidumap;2 3 ImportAndroid.os.Bundle;4 Importandroid.support.v7.app.AppCompatActivity;5 6 Importcom.baidu.location.BDLocation;7 ImportCom.baidu.location.BDLocationListener;8 Importcom.baidu.location.LocationClient;9 Importcom.baidu.location.LocationClientOption;Ten ImportCom.baidu.mapapi.SDKInitializer; One ImportCom.baidu.mapapi.map.BaiduMap; A ImportCom.baidu.mapapi.map.MapStatus; - Importcom.baidu.mapapi.map.MapStatusUpdateFactory; - ImportCom.baidu.mapapi.map.MapView; the ImportCom.baidu.mapapi.map.MyLocationData; - ImportCom.baidu.mapapi.model.LatLng; -  -  Public classMainactivityextendsappcompatactivity { +  -     PrivateMapview Mmapview; +     PrivateBaidumap Mbaidumap; A     Privatelocationclient mlocclient; at      PublicMylocationlistenner MyListener =NewMylocationlistenner (); -     BooleanIsfirstloc =true;//whether to first position -  -  - @Override -     protected voidonCreate (Bundle savedinstancestate) { in         Super. OnCreate (savedinstancestate); -         //initializes the context information before using each component of the SDK, passing in the ApplicationContext to         //Note that this method should be implemented before setcontentview the method + sdkinitializer.initialize (Getapplicationcontext ()); - Setcontentview (r.layout.activity_main); the         //Get map Control Reference *Mmapview =(Mapview) Findviewbyid (R.id.bmapview);

/************************************ from here is copied in the Locationdemo.java, and then according to the error, what is missing what ******************************** ******************************/
$Mbaidumap =Mmapview.getmap ();Panax Notoginseng //turn on positioning layers -Mbaidumap.setmylocationenabled (true); the //Positioning Initialization +Mlocclient =NewLocationclient ( This); A Mlocclient.registerlocationlistener (mylistener); thelocationclientoption option =Newlocationclientoption (); +Option.setopengps (true);//Turn on GPS -Option.setcoortype ("Bd09ll");//set the coordinate type $Option.setscanspan (1000); $ mlocclient.setlocoption (option); - Mlocclient.start ();
/****************************************************************************************************/
- } the - /**Wuyi * Locate the SDK listener function the */ - Public classMylocationlistennerImplementsBdlocationlistener { Wu - @Override About Public voidonreceivelocation (bdlocation location) { $ //map view is not processed in the new receive location after it is destroyed - if(Location = =NULL|| Mmapview = =NULL) { - return; - } AMylocationdata Locdata =NewMylocationdata.builder () + . Accuracy (Location.getradius ()) the //This sets the direction information the developer obtains, clockwise 0-360 -. Direction (100). Latitude (Location.getlatitude ()) $ . Longitude (Location.getlongitude ()). build (); the Mbaidumap.setmylocationdata (locdata); the if(isfirstloc) { theIsfirstloc =false; theLATLNG LL =Newlatlng (Location.getlatitude (), - location.getlongitude ()); inMapstatus.builder Builder =NewMapstatus.builder (); theBuilder.target (LL). Zoom (18.0f); the Mbaidumap.animatemapstatus (Mapstatusupdatefactory.newmapstatus (Builder.build ())); About } the } the the Public voidOnreceivepoi (bdlocation poilocation) { + } - } the Bayi @Override the protected voidOnDestroy () { the Super. OnDestroy (); - //Implement Mmapview.ondestroy () when activity executes OnDestroy to achieve map life cycle management - Mmapview.ondestroy (); the } the @Override the protected voidOnresume () { the Super. Onresume (); - //perform mmapview. Onresume () When activity executes Onresume, enabling map Lifecycle management the Mmapview.onresume (); the } the @Override94 protected voidOnPause () { the Super. OnPause (); the //perform mmapview. OnPause () When activity executes OnPause, enabling map Lifecycle management the mmapview.onpause ();98 } About}

Above, run the code to show your current location on the map

Note: If the running code appears to be positioned in a blue area, when you narrow the map, you will find that it is located in the ocean or somewhere else.

1. Check each step for errors, especially jar packages, there are no references

2. If it's a simulator, replace it with a real machine test.

3. If it is a real machine, check whether the phone system is 6.0 and above, if yes: in Build.gradle, change targetsdkversion to 22

Android Baidu map API Integration II "positioning"

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.