Android-Baidu Map call (basic version), android-Map

Source: Internet
Author: User

Android-Baidu Map call (basic version), android-Map

Previously I wrote an Android-Baidu Map call (retrieval function) because my company's projects are useful and I made some simple sharing.

This article briefly introduces the basic map usage of Baidu map.
Now the Android SDK version provided by Baidu developer platform is-Android sdk v 3.4.0.

1. What is Baidu map Android SDK? (Let's take a look at the official website)



2. How to obtain the custom Baidu map SDK.
Developers can download the latest map SDK from the Baidu map Android SDK download page,

Is: http://developer.baidu.com/map/index.php? Title = androidsdk/sdkandev-download

3. Basic Map.


1. display a hundred maps
The Baidu map SDK provides developers with an interface to conveniently display Baidu map data. You can use Baidu map data in your application in the following steps:
Step 1: Create and configure a project (For details, refer to the project configuration section );
Step 2: add the development key and required permissions to AndroidManifest;

1) Add a development Key to the application

<Application> <meta-data android: name = "com. baidu. lbsapi. API_KEY" android: value = "Developer key"/> </application>


2) Add required permissions
<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" />
Step 3: Add a map control to the layout xml file;

<com.baidu.mapapi.map.MapView      android:id="@+id/bmapView"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:clickable="true" />
Step 4: Initialize the Context global variable referenced by the SDK when creating an application:

Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // 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. initialize (getApplicationContext (); setContentView (R. layout. activity_main );}}

Note:You must call each function component of the SDK before using it.

SDKInitializer. initialize (getApplicationContext ();

Step 5: create a map Activity and manage the map lifecycle;


Public class MainActivity extends Activity {MapView mMapView = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // 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. initialize (getApplicationContext (); setContentView (R. layout. activity_main); // obtain the map control reference mMapView = (MapView) findViewById (R. id. bmapView) ;}@ Override protected void onDestroy () {super. onDestroy (); // execute mMapView when onDestroy is executed in activity. onDestroy () to implement mMapView for map lifecycle management. 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 ();}}
After completing the preceding steps, run the program to display the following map in your application:




2. Basic Map


(1) map type

MMapView = (MapView) findViewById (R. id. bmapView); mBaiduMap = mMapView. getMap (); // common map mBaiduMap. setMapType (BaiduMap. MAP_TYPE_NORMAL); // satellite map mBaiduMap. setMapType (BaiduMap. MAP_TYPE_SATELLITE );
(2) Real-time traffic map

MMapView = (MapView) findViewById (R. id. bmapView); mBaiduMap = mMapView. getMap (); // enable traffic map mBaiduMap. setTrafficEnabled (true );
(3) urban heat map

MMapView = (MapView) findViewById (R. id. bmapView); mBaiduMap = mMapView. getMap (); // enable traffic map mBaiduMap. setBaiduHeatMapEnabled (true );
(4), flag covering

// Define Maker coordinate point LatLng point = new LatLng (39.963175, 116.400244); // construct the Marker icon BitmapDescriptor bitmap = BitmapDescriptorFactory. fromResource (R. drawable. icon_marka); // 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 );


(5) text covering

// Defines the coordinate point LatLng llText = new LatLng (39.86923, 116.397428) displayed in the text; // constructs the text Option object, adds OverlayOptions textOption = new TextOptions () to a map (). bgColor (0xAAFFFF00 ). fontSize (24 ). fontColor (0xFFFF00FF ). text ("Baidu map SDK "). rotate (-30 ). position (llText); // Add the Text object to the map and display the mBaiduMap. addOverlay (textOption );

(6), pop-up window covering

// Create the view Button button shown in InfoWindow = new Button (getApplicationContext (); button. setBackgroundResource (R. drawable. popup); // defines the coordinate point LatLng pt = new LatLng (39.86923, 116.397428) used to display the InfoWindow; // creates the InfoWindow, transmits the view, geographic coordinates, y axis offset InfoWindow mInfoWindow = new InfoWindow (button, pt,-47); // display InfoWindow mBaiduMap. showInfoWindow (mInfoWindow );

(7). Search Result cover

For the retrieval function module (such as POI retrieval and line planning), the map SDK also provides corresponding overlay to quickly display the result information. These methods are open-source. developers can customize them based on their actual needs.

You can use the retrieval result cover to display the POI search results as follows:

Step 1: Construct a custom PoiOverlay class;

private class MyPoiOverlay extends PoiOverlay {      public MyPoiOverlay(BaiduMap baiduMap) {          super(baiduMap);      }      @Override      public boolean onPoiClick(int index) {          super.onPoiClick(index);          return true;      }  }

Step 2: Add a custom PoiOverlay to the POI retrieval callback interface;

Public void onGetPoiResult (PoiResult result) {if (result = null | result. error = SearchResult. ERRORNO. RESULT_NOT_FOUND) {return;} if (result. error = SearchResult. ERRORNO. NO_ERROR) {mBaiduMap. clear (); // create PoiOverlay overlay = new MyPoiOverlay (mBaiduMap); // set overlay to handle the Click Event mBaiduMap. setOnMarkerClickListener (overlay); // sets the overlay of PoiOverlay data. setData (result); // Add PoiOverlay to the map overlay. addToMap (); overlay. zoomToSpan (); return ;}}
The running result is as follows:


Sharing ends now ~ If you have any questions, you can leave a message and communicate with each other ~~

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.