Use of Baidu map under Android Studio (2) -- positioning service, androidstudio

Source: Internet
Author: User

Use of Baidu map under Android Studio (2) -- positioning service, androidstudio

In the previous chapter (http://www.cnblogs.com/jerehedu/p/4891216.html) we have completed the establishment of the environment, this chapter we will study how to use.

Step 1: Add the following permissions to the xml file:
<Uses-permission android: name = "android. permission. INTERNET "/> <uses-permission android: name =" com. android. launcher. permission. READ_SETTINGS "/> <uses-permission android: name =" android. permission. WAKE_LOCK "/> <uses-permission android: name =" android. permission. CHANGE_WIFI_STATE "/> <uses-permission android: name =" android. permission. ACCESS_WIFI_STATE "/> <uses-permission android: name =" android. permission. WRITE_EXTERNAL_STORAGE "/> <uses-permission android: name =" android. permission. WRITE_SETTINGS "/> <! -- This permission is used for network positioning --> <uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/> <! -- This permission is used to access GPS positioning --> <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/> <! -- Used to access Wi-Fi network information. wifi information is used to locate the network --> <uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"/> <! -- Obtains carrier information, which is used to support interfaces related to carrier information --> <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/> <! -- This permission is used to obtain the permission for wifi, And wifi information is used to locate the network --> <uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"/> <! -- Used to read the current status of the mobile phone --> <uses-permission android: name = "android. permission. READ_PHONE_STATE"/> <! -- Write extended storage and write data to the expansion card to write offline positioning data --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/> <! -- Access the network and access the INTERNET to locate the network --> <uses-permission android: name = "android. permission. INTERNET"/> <! -- SD card read permission, users write offline positioning data --> <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/> <! -- Allow applications to read low-level system log files --> <uses-permission android: name = "android. permission. READ_LOGS "/> <uses-permission android: name =" android. permission. INTERNET "/> <uses-permission android: name =" com. android. launcher. permission. READ_SETTINGS "/> <uses-permission android: name =" android. permission. WAKE_LOCK "/> <uses-permission android: name =" android. permission. CHANGE_WIFI_STATE "/> <uses-permission android: name =" andro Id. permission. ACCESS_WIFI_STATE "/> <uses-permission android: name =" android. permission. WRITE_EXTERNAL_STORAGE "/> <uses-permission android: name =" android. permission. WRITE_SETTINGS "/> <! -- This permission is used for network positioning --> <uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/> <! -- This permission is used to access GPS positioning --> <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/> <! -- Used to access Wi-Fi network information. wifi information is used to locate the network --> <uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"/> <! -- Obtains carrier information, which is used to support interfaces related to carrier information --> <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/> <! -- This permission is used to obtain the permission for wifi, And wifi information is used to locate the network --> <uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"/> <! -- Used to read the current status of the mobile phone --> <uses-permission android: name = "android. permission. READ_PHONE_STATE"/> <! -- Write extended storage and write data to the expansion card to write offline positioning data --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/> <! -- Access the network and access the INTERNET to locate the network --> <uses-permission android: name = "android. permission. INTERNET"/> <! -- SD card read permission, users write offline positioning data --> <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/> <! -- Allow applications to read low-level system log files --> <uses-permission android: name = "android. permission. READ_LOGS"/>
Step 2: Add a map control to the layout File
<LinearLayout 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: orientation = "vertical" tools: context = ". mainActivity "> <TextView android: text =" Baidu map "android: layout_width =" match_parent "android: gravity =" center "android: layout_height =" wrap_content "android: textSize = "25sp" android: id = "@ + id/textView"/> <com. baidu. mapapi. map. mapView android: id = "@ + id/baiDuMv" android: layout_width = "match_parent" android: layout_height = "match_parent" android: clickable = "true"/> </LinearLayout>
Step 3: add the initialization statement to the OnCreate method of the Activity.
// This method implements SDKInitializer. initialize (getApplicationContext () before setContentView ());
Step 4: Add lifecycle management to save money.
@ Override protected void onStop () {locationClient. stop (); super. onStop () ;}@ Override protected void onDestroy () {super. onDestroy (); // execute mMapView when onDestroy is executed in activity. onDestroy () mMapView. onDestroy (); mMapView = null;} @ Override protected void onResume () {super. onResume (); // execute mMapView when onResume is executed in the activity. onResume () mMapView. onResume () ;}@ Override protected void onPause () {super. onPause (); // execute mMapView when onPause is executed in activity. onPause () mMapView. onPause ();}

:

The complete code is as follows:

Package com. jerehedu. administrator. baidumapapplication; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import com. baidu. location. BDLocation; import com. baidu. location. BDLocationListener; import com. baidu. location. locationClient; import com. baidu. location. locationClientOption; import com. baidu. mapapi. SDKInitializer; import com. baidu. mapapi. map. ba IduMap; import com. baidu. mapapi. map. bitmapDescriptor; import com. baidu. mapapi. map. bitmapDescriptorFactory; import com. baidu. mapapi. map. mapStatusUpdate; import com. baidu. mapapi. map. mapStatusUpdateFactory; import com. baidu. mapapi. map. mapView; import com. baidu. mapapi. map. myLocationConfigeration; import com. baidu. mapapi. map. myLocationData; import com. baidu. mapapi. model. latLng; public class MainActivity extends Activity {private MapView mMapView; private LocationClient locationClient; private BaiduMap baiduMap; private boolean firstLocation; private BitmapDescriptor mCurrentMarker; private parameter config; @ Override protected void onCreate (Bundle privilege) {super. onCreate (savedInstanceState); // This method must be implemented before the setContentView method. initialize (getApplicationContext (); setCo NtentView (R. layout. activity_main); mMapView = (MapView) findViewById (R. id. baiDuMv); baiduMap = mMapView. getMap (); MapStatusUpdate msu = MapStatusUpdateFactory. zoomTo (15f); baiduMap. setMapStatus (msu); // positioning initialization locationClient = new LocationClient (this); firstLocation = true; // you can specify the LocationClientOption option for the LocationClientOption (); option. setLocationMode (LocationClientOption. locationMode. H Ight_Accuracy); option. setOpenGps (true); option. setCoorType ("bd09ll"); // sets the coordinate type option. setScanSpan (1000); locationClient. setLocOption (option); // set the custom icon BitmapDescriptor myMarker = BitmapDescriptorFactory. fromResource (R. drawable. navi_map); MyLocationConfigeration config = new MyLocationConfigeration (MyLocationConfigeration. locationMode. FOLLOWING, true, myMarker); locationClient. registerlow.o NListener (new BDLocationListener () {@ Override public void onReceiveLocation (BDLocation location) {// After map view is destroyed, if (location = null | mMapView = null) return is not processed at the newly received location; // construct the location data MyLocationData locData = new MyLocationData. builder (). accuracy (location. getRadius () // set the direction information obtained by the developer, Which is 0-360 clockwise. direction (100 ). latitude (location. getLatitude ()). longpolling (location. getlongpolling ()). build (); // set Bit Data baiduMap. setMyLocationData (locData); // when positioning for the first time, move the map location to the current location if (firstLocation) {firstLocation = false; LatLng xy = new LatLng (location. getLatitude (), location. getlongpolling (); MapStatusUpdate status = MapStatusUpdateFactory. newLatLng (xy); baiduMap. animateMapStatus (status) ;}}) ;}@ Override protected void onStart () {// to display the position icon, you must enable the layer to locate the baiduMap. setMyLocationEnabled (true); if (! LocationClient. isStarted () {locationClient. start ();} super. onStart () ;}@ Override protected void onStop () {// close the layer to locate the baiduMap. setMyLocationEnabled (false); locationClient. stop (); super. onStop () ;}@ Override protected void onDestroy () {super. onDestroy (); // execute mMapView when onDestroy is executed in activity. onDestroy () mMapView. onDestroy (); mMapView = null;} @ Override protected void onResume () {super. onResume (); // execute mMapView when onResume is executed in the activity. onResume () mMapView. onResume () ;}@ Override protected void onPause () {super. onPause (); // execute mMapView when onPause is executed in activity. onPause () mMapView. onPause ();}}

In this way, the simple positioning and display functions are implemented. Of course, more functions need to be learned from the API documentation.

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.