Basic use of Baidu map Android SDK-tagging (Marker)
Mark (Marker) is the most commonly used map cover for developers. Today we will introduce you to some of the most basic usage methods of mark (Marker!
Goals:
1. Create a basic map page;
2. Add a Marker at the center of the map;
3. Implement the Click Event Response of Marker;
4. Add and flexibly Delete Marker;
To achieve the above objectives, we will take the following steps.
Step 1: Create a project and import the Baidu map Android SDK;
The specific method for creating a project (omitted ). This example is mainly used to implement map and related covering functions. Therefore, I only use the basic map function module provided by the SDK development resource download platform.
Correspondingly: http://lbsyun.baidu.com/sdk/download
Check:
After selecting, click Download to correctly import the downloaded development kit to the development project.
Step 2: Apply for a developer key;
After the project is created, go to the API console and apply for the corresponding development key to prepare for the specific development work.
Specific Application Method and precautions, please refer to: http://blog.csdn.net/callmesen/article/details/39523767 here will not go into details.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + tdrI/large + 6zcv50OjIqM/eo7s8L3A + large + 1 tCjrMztvNO/large + zO2807 + qt6LD2NS/large + large = "brush: java;"> Note: In the application.
Add the following permissions:
Step 4: Create a basic map;
Add the mapview control to the xml file as follows:
Initialize the map SDK in java code to display the basic map:
// Initialize the map SDKSDKInitializer. initialize (getApplicationContext (); setContentView (R. layout. activity_main); // bind mapview control mapView = (MapView) findViewById (R. id. bmapView );
Step 5. Obtain the map center and add a Marker object;
The core code is as follows:
BaiduMap = mapView. getMap (); LatLng latLng = baiduMap.getMapStatus().tar get; // prepare the marker image BitmapDescriptor bitmap = BitmapDescriptorFactory. fromResource (R. drawable. ic_launcher); // prepare marker option to add marker using markerOptions = new MarkerOptions (). icon (bitmap ). position (latLng); // obtain the added marker to facilitate subsequent operations. marker = (Marker) baiduMap. addOverlay (markerOptions );
Step 6: implement the Click Event Response of Marker;
Use the map listening method of the baidumap object to implement click response for the marker. The core code is as follows:
// Add and click the corresponding event baiduMap for the marker. setOnMarkerClickListener (new OnMarkerClickListener () {@ Overridepublic boolean onMarkerClick (Marker arg0) {// TODO Auto-generated method stubToast. makeText (getApplicationContext (), "Marker is clicked! ", Toast. LENGTH_SHORT). show (); return false ;}});
Step 7: Use the Button control to control the addition and deletion of Marker;
Here, a button is used to control the addition or deletion of a marker on the map. First, a button control is added to the xml file:
Add the Click Event Response of this button in java code. The core code is as follows:
// Button control addition and deletion of buttonbutton. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubif (button. getText () = "add Marker") {marker = (Marker) baiduMap. addOverlay (markerOptions); button. setText ("delete Marker");} else {marker. remove (); button. setText ("add Marker ");}}});
With the preceding operations, you can add corresponding marker, click response, and delete marker.