Android: Baidu map app and android Baidu map app

Source: Internet
Author: User

Android: Baidu map app and android Baidu map app

Based on Baidu api

Copy

<com.baidu.mapapi.map.MapView      android:id="@+id/bmapView"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:clickable="true" />

Variable to be defined

private BaiduMap mBaiduMap;    private MapView mMapView;    private LocationClient mLocationClient;    private boolean isFirstIn = true;

Initialization

mMapView= (MapView) findViewById(R.id.bmapView);        mBaiduMap = mMapView.getMap();

Functions implemented by each part
1.

// Initialize the context information before using the SDK components and pass in the ApplicationContext // note that this method must be implemented before the setContentView method (getApplicationContext ());

Binds the life cycle of a map to the activity.

@ Override protected void onStart () {super. onStart (); mBaiduMap. setMyLocationEnabled (true); if (! MLocationClient. isStarted () mLocationClient. start () ;}@ Override protected void onStop () {// TODO Auto-generated method stub super. onStop () ;}@ Override protected void onDestroy () {// TODO Auto-generated method stub super. onDestroy (); mMapView. onDestroy () ;}@ Override protected void onResume () {super. onResume (); // execute mMapView when onResume is executed in the activity. onResume () to implement mMapView for map lifecycle management. onResume () ;}@ Override protected void onPause () {super. onPause (); // execute mMapView when onPause is executed in activity. onPause () enables mMapView for map lifecycle management. onPause ();}

Implement option click conversion map Mode

@ Override public boolean onCreateOptionsMenu (Menu menu) {// TODO Auto-generated method stub menu. add (0, 1, 0, "normal map"); menu. add (0, 2, 0, "satellite map"); menu. add (0, 3, 0, "real-time traffic (off)"); return super. onCreateOptionsMenu (menu );}

Set option click events

@ Override public boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case 1: // common map mBaiduMap. setMapType (BaiduMap. MAP_TYPE_NORMAL); break; case 2: // satellite map mBaiduMap. setMapType (BaiduMap. MAP_TYPE_SATELLITE); break; case 3: if (mBaiduMap. isTrafficEnabled () {mBaiduMap. setTrafficEnabled (false); item. setTitle ("real-time traffic (off)");} else {mBaiduMap. setTrafficEnabled (true); item. setTitle ("real-time traffic (on)");} break;} return super. onOptionsItemSelected (item );}

Add an identifier to a map

// Define Maker coordinate point LatLng point = new LatLng (39.963175, 116.400244); // construct the Marker icon BitmapDescriptor bitmap = BitmapDescriptorFactory. fromResource (R. drawable. ic_launcher); // construct a MarkerOption to add Marker OverlayOptions option = new MarkerOptions () to the map (). position (point ). icon (bitmap); // Add a Marker to the map and display the mBaiduMap. addOverlay (option );

Set gps Positioning
Classes to be used
LocationClient
BDLocationListener

(1)

MyLocationListener myLocationListener = new MyLocationListener (); mLocationClient = new LocationClient (getApplicationContext (); // declare the LocationClient class mLocationClient. registerLocationListener (myLocationListener); // register a listener

(2) Use LocationClientOption to set positioning attributes

LocationClientOption option = new LocationClientOption (); option. setLocationMode (LocationMode. hight_Accuracy); // sets the positioning mode option. setCoorType ("bd09ll"); // The return result is Baidu longitude and latitude. The default value is gcj02 option. setScanSpan (1000); // set the interval between initiating a request for locating to 5000 ms option. setIsNeedAddress (true); // The returned positioning result contains the address information option. setNeedDeviceDirect (true); // The returned positioning result contains the direction of the mobile phone head to mLocationClient. setLocOption (option );

(3) Listener implementation

Private class MyLocationListener implements BDLocationListener {@ Override public void onid elocation (BDLocation location) {// TODO Auto-generated method stub MyLocationData data = new MyLocationData. builder () // set the longitude and latitude. accuracy (location. getRadius ()). latitude (location. getLatitude ()). longpolling (location. getlongpolling ()). build (); mBaiduMap. setMyLocationData (data); // identify the first entry and locate the current location if (isFirstIn) {LatLng latLng = new LatLng (location. getLatitude (), location. getlongpolling (); MapStatusUpdate msu = MapStatusUpdateFactory. newLatLng (latLng); mBaiduMap. animateMapStatus (msu); Toast. makeText (getApplicationContext (), location. getAddrStr (), 3000 ). show (); isFirstIn = false ;}}}

Permissions to be added

<! -- This permission is used for network positioning --> <uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"> </uses-permission> <! -- This permission is used to access GPS positioning --> <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"> </uses-permission> <! -- Used to access Wi-Fi network information. wifi information is used to locate the network --> <uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"> </uses-permission> <! -- Obtains carrier information, which is used to support interfaces related to carrier information --> <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"> </uses-permission> <! -- 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 "> </uses-permission> <! -- Used to read the current status of the mobile phone --> <uses-permission android: name = "android. permission. READ_PHONE_STATE"> </uses-permission> <! -- Write extended storage and write data to the expansion card for writing offline positioning data --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE "> </uses-permission> <! -- 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"> </uses-permission> <! -- Allow applications to read low-level system log files --> <uses-permission android: name = "android. permission. READ_LOGS "> </uses-permission> <uses-permission android: name =" android. permission. GET_ACCOUNTS "/> <uses-permission android: name =" android. permission. USE_CREDENTIALS "/> <uses-permission android: name =" android. permission. MANAGE_ACCOUNTS "/> <uses-permission android: name =" android. permission. AUTHENTICATE_ACCOUNTS "/> <uses-permission android: name =" android. permission. ACCESS_NETWORK_STATE "/> <uses-permission android: name =" android. permission. INTERNET "/> <uses-permission android: name =" com. android. launcher. permission. READ_SETTINGS "/> <uses-permission android: name =" android. permission. CHANGE_WIFI_STATE "/> <uses-permission android: name =" android. permission. ACCESS_WIFI_STATE "/> <uses-permission android: name =" android. permission. READ_PHONE_STATE "/> <uses-permission android: name =" android. permission. WRITE_EXTERNAL_STORAGE "/> <uses-permission android: name =" android. permission. BROADCAST_STICKY "/> <uses-permission android: name =" android. permission. WRITE_SETTINGS "/>

The

 <meta-data            android:name="com.baidu.lbsapi.API_KEY"            android:value="OSzBVaOV08hT2o7yNBIq4NFS" />        <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote">    </service>

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.