Baidu map android sdk3.5 allows you to locate and jump to the specified coordinates and add tags

Source: Internet
Author: User

Baidu map android sdk3.5 allows you to locate and jump to the specified coordinates and add tags

A few days ago, I downloaded the new Baidu map sdk, version 3.5, and found that the Baidu map api has changed significantly.

 

Similar to version 3.0

However, setting the map center and adding the maker tag have greatly changed.

Set map center point

// Define the map state zoom to indicate the zoom Level 3-18
MapStatus mMapStatus = new MapStatus.Builder().tar get (cenpt)
. Zoom (14). build ();
// Define the MapStatusUpdate object to describe the changes in the MAP status
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory
. NewMapStatus (mMapStatus );
// Change the MAP status
// Enable the positioning Layer
MMapView. getMap (). setMapStatus (mMapStatusUpdate );

Add maker tag

// Defines the coordinate point of the Maker.
// Construct the Marker icon
BitmapDescriptor bitmap = BitmapDescriptorFactory
. FromResource (R. drawable. ic_launcher );
// Construct a MarkerOption to add a Marker to a map
OverlayOptions option = new MarkerOptions (). position (cenpt). icon (
Bitmap );
// Add a Marker to the map and display it
MMapView. getMap (). clear ();
MMapView. getMap (). addOverlay (option );
MLocationClient. stop ();

 

 

 

 

 

 

 

The complete code is as follows:

Public class MapActivity extends BaseActivity {
MapView mMapView;


Public LocationClient mLocationClient = null;
Public BDLocationListener myListener = new MyLocationListener ();


@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SDKInitializer. initialize (getApplicationContext ());
SetContentView (R. layout. activity_mapview );
MMapView = (MapView) findViewById (R. id. bmapView );


MLocationClient = new LocationClient (getApplicationContext (); // declare the LocationClient class
MLocationClient. registerLocationListener (myListener); // registers the listener Function
MLocationClient. start ();
}


@ Override
Protected void onDestroy (){
Super. onDestroy ();
// Execute mMapView. onDestroy () When onDestroy is executed in the activity to implement map lifecycle management.
MMapView. onDestroy ();
MLocationClient. stop ();
}


@ Override
Protected void onResume (){
Super. onResume ();
// Execute mMapView. onResume () When onResume is executed in the activity to implement map lifecycle management
MMapView. onResume ();
}


@ Override
Protected void onPause (){
Super. onPause ();
// Execute mMapView. onPause () When onPause is executed in the activity to implement map lifecycle management.
MMapView. onPause ();
}


Public class MyLocationListener implements BDLocationListener {
@ Override
Public void onReceiveLocation (BDLocation location ){
If (location = null)
Return;
StringBuffer sb = new StringBuffer (256 );
Sb. append (time :);
Sb. append (location. getTime ());
Sb. append (error code :);
Sb. append (location. getLocType ());
Sb. append (latitude :);
Sb. append (location. getLatitude ());
Sb. append (lontitude :);
Sb. append (location. getlongpolling ());
Sb. append (radius :);
Sb. append (location. getRadius ());
If (location. getLocType () = BDLocation. TypeGpsLocation ){
Sb. append (speed :);
Sb. append (location. getSpeed ());
Sb. append (satellite :);
Sb. append (location. getSatelliteNumber ());
} Else if (location. getLocType () = BDLocation. TypeNetWorkLocation ){
Sb. append (addr :);
Sb. append (location. getAddrStr ());
}
System. out. println (+ sb. toString ());
LatLng cenpt = new LatLng (location. getLatitude (),
Location. getlongpolling ());
// Define the map state zoom to indicate the zoom Level 3-18
MapStatus mMapStatus = new MapStatus.Builder().tar get (cenpt)
. Zoom (14). build ();
// Define the MapStatusUpdate object to describe the changes in the MAP status
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory
. NewMapStatus (mMapStatus );
// Change the MAP status
// Enable the positioning Layer
MMapView. getMap (). setMapStatus (mMapStatusUpdate );




// Defines the coordinate point of the Maker.
// Construct the Marker icon
BitmapDescriptor bitmap = BitmapDescriptorFactory
. FromResource (R. drawable. ic_launcher );
// Construct a MarkerOption to add a Marker to a map
OverlayOptions option = new MarkerOptions (). position (cenpt). icon (
Bitmap );
// Add a Marker to the map and display it
MMapView. getMap (). clear ();
MMapView. getMap (). addOverlay (option );
MLocationClient. stop ();
}
}


}

 

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.