Study Notes for Android: Baidu Map

Source: Internet
Author: User

Before using the API, You need to obtain the Baidu map mobile API Key, which is related to the program name you reference the API. For details, see apply for API KEYhttp: // dev.baidu.com/wiki/static/imap/key/

Baidu map API supports Android 1.5 and later systems.
 
 
<1> Add an API to the Andoid Project
 
Must appear download API Development Kit: Click to download http://www.bkjia.com/uploadfile/2012/0305/20120305100544520.zip
 
 
The extracted files include baidumapapi. jar and libBMapApiEngine. so. Create the ibs \ armeabi directory under the project root directory and copy it to its directory.
 
Select "Add JARs" from Project Properties> Java Build Path> Libraries, select baidumapapi. jar, and return the result. In this way, you can use the API in your program.
 
<2> Add the permission in Manifest
 
This is not something that many people will forget. Pay attention to it.
 
[Java] <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"> </uses-permission>
<Uses-permission android: name = "android. permission. INTERNET"> </uses-permission>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"> </uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"> </uses-permission>
<Uses-permission android: name = "android. permission. INTERNET"> </uses-permission>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"> </uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"> </uses-permission>
<3> Add Android support in Manifest
 
[Java] <supports-screens android: largeScreens = "true"
Android: normalScreens = "true" android: smallScreens = "true"
Android: resizeable = "true" android: anyDensity = "true"/>
<Uses-sdk android: minSdkVersion = "3"> </uses-sdk>
<Supports-screens android: largeScreens = "true"
Android: normalScreens = "true" android: smallScreens = "true"
Android: resizeable = "true" android: anyDensity = "true"/>
<Uses-sdk android: minSdkVersion = "3"> </uses-sdk>
<4> inherit com. baidu. mapapi. MapActivity from the created map Activity and import related classes.
 
Some import-related classes in the Code may not be needed. This depends on your function.
 
You do not need to import these data. Use the "import (" alt +/"shortcut key)
 
[Java] import java. util. ArrayList;
Import java. util. List;

Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Paint;
Import android. graphics. Point;
Import android. graphics. drawable. Drawable;
Import android. location. Location;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. widget. Toast;

Import com. baidu. mapapi. BMapManager;
Import com. baidu. mapapi. GeoPoint;
Import com. baidu. mapapi. ItemizedOverlay;
Import com. baidu. mapapi. LocationListener;
Import com. baidu. mapapi. MKAddrInfo;
Import com. baidu. mapapi. MKDrivingRouteResult;
Import com. baidu. mapapi. MKGeneralListener;
Import com. baidu. mapapi. MKLocationManager;
Import com. baidu. mapapi. MKPlanNode;
Import com. baidu. mapapi. MKPoiResult;
Import com. baidu. mapapi. MKSearch;
Import com. baidu. mapapi. MKSearchListener;
Import com. baidu. mapapi. MKTransitRouteResult;
Import com. baidu. mapapi. MKWalkingRouteResult;
Import com. baidu. mapapi. MapActivity;
Import com. baidu. mapapi. MapController;
Import com. baidu. mapapi. MapView;
Import com. baidu. mapapi. MyLocationOverlay;
Import com. baidu. mapapi. Overlay;
Import com. baidu. mapapi. OverlayItem;
Import com. baidu. mapapi. PoiOverlay;
Import com. baidu. mapapi. RouteOverlay;
Import com. baidu. mapapi. TransitOverlay;

Public class MyMapActivity extends MapActivity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}

@ Override
Protected boolean isRouteDisplayed (){
Return false;
}
}
Import java. util. ArrayList;
Import java. util. List;
 
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Paint;
Import android. graphics. Point;
Import android. graphics. drawable. Drawable;
Import android. location. Location;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. widget. Toast;
 
Import com. baidu. mapapi. BMapManager;
Import com. baidu. mapapi. GeoPoint;
Import com. baidu. mapapi. ItemizedOverlay;
Import com. baidu. mapapi. LocationListener;
Import com. baidu. mapapi. MKAddrInfo;
Import com. baidu. mapapi. MKDrivingRouteResult;
Import com. baidu. mapapi. MKGeneralListener;
Import com. baidu. mapapi. MKLocationManager;
Import com. baidu. mapapi. MKPlanNode;
Import com. baidu. mapapi. MKPoiResult;
Import com. baidu. mapapi. MKSearch;
Import com. baidu. mapapi. MKSearchListener;
Import com. baidu. mapapi. MKTransitRouteResult;
Import com. baidu. mapapi. MKWalkingRouteResult;
Import com. baidu. mapapi. MapActivity;
Import com. baidu. mapapi. MapController;
Import com. baidu. mapapi. MapView;
Import com. baidu. mapapi. MyLocationOverlay;
Import com. baidu. mapapi. Overlay;
Import com. baidu. mapapi. OverlayItem;
Import com. baidu. mapapi. PoiOverlay;
Import com. baidu. mapapi. RouteOverlay;
Import com. baidu. mapapi. TransitOverlay;
 
Public class MyMapActivity extends MapActivity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
 
@ Override
Protected boolean isRouteDisplayed (){
Return false;
}
} <5> Add a map control to the layout xml
 
[Java] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: text = "@ string/hello"/>
<Com. baidu. mapapi. MapView android: id = "@ + id/bmapsView"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
Android: clickable = "true"/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: text = "@ string/hello"/>
<Com. baidu. mapapi. MapView android: id = "@ + id/bmapsView"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
Android: clickable = "true"/>
</LinearLayout>
<6> initialize a map Activity and define the variable BMapManager mapManager = null in the map Activity. Add the following code to the onCreate method, replace the Key you applied for with "my Key ":
[Java] mapManager = new BMapManager (getApplication ());
MapManager. init (key, null );
Super. initMapActivity (mapManager );

MapView mapView = (MapView) findViewById (R. id. mapsView );
MapView. setBuiltInZoomControls (true); // you can enable the built-in zoom control.

MapController mapController = mapView. getController (); // get control of mMapView, which can be used to control and drive translation and Scaling
GeoPoint point = new GeoPoint (int) (39.915*1E6 ),
(Int) (116.404*1E6); // construct a GeoPoint with the given latitude and longitude, in the unit of (degree * 1E6)
MapController. setCenter (point); // sets the map center point.
MapController. setZoom (12); // sets the map zoom level.
MapManager = new BMapManager (getApplication ());
MapManager. init (key, null );
Super. initMapActivity (mapManager );

MapView mapView = (MapView) findViewById (R. id. mapsView );
MapView. setBuiltInZoomControls (true); // you can enable the built-in zoom control.

MapController mapController = mapView. getController (); // get control of mMapView, which can be used to control and drive translation and Scaling
GeoPoint point = new GeoPoint (int) (39.915*1E6 ),
(Int) (116.404*1E6); // construct a GeoPoint with the given latitude and longitude, in the unit of (degree * 1E6)
MapController. setCenter (point); // sets the map center point.
MapController. setZoom (12); // sets the map zoom level.
 
Override:
 
 
[Java] @ Override protected void onDestroy (){

If (mapManager! = Null ){
MapManager. destroy ();
MapManager = null;
}
Super. onDestroy ();
}
@ Override
Protected void onPause (){
If (mapManager! = Null ){
MapManager. stop ();
}
Super. onPause ();
}
@ Override
Protected void onResume (){
If (mapManager! = Null ){
MapManager. start ();
}
Super. onResume ();
}

@ Override
Protected void onDestroy (){
If (mapManager! = Null ){
MapManager. destroy ();
MapManager = null;
}
Super. onDestroy ();
}
@ Override
Protected void onPause (){
If (mapManager! = Null ){
MapManager. stop ();
}
Super. onPause ();
}
@ Override
Protected void onResume (){
If (mapManager! = Null ){
MapManager. start ();
}
Super. onResume ();
}



From sunset hut

 

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.