I recently used the Baidu map API to share my experience here. To use Baidu API and Google first go to Baidu map API site to apply for key, application address is as follows: http://dev.baidu.com/wiki/static/imap/key/, after the application can be used, but Baidu API is added to the project as a third-party plug-in, so you also need to download Baidu map API jar package, as follows: http://dev.baidu.com/wiki/imap/index.php? Title = Android % E5 % B9 % B3 % E5 % 8f % B0/% E7 % 9B % B8 % E5 % 85% B3 % E4 % B8 % 8B % E8 % BD
Open the downloaded package and you will find a jar package and. so file, you need to copy these two files to the libs folder under the project root directory, and create a new armeabi folder, because. the so file needs to be placed under this folder. For detailed steps, refer to Baidu mobile map SDK usage instructions. At last, you need to add permissions and versions to the manifest file as follows:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> <uses-permission android:name="android.permission.CHANGE_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.MOUNT_UNMOUNT_FILESYSTEMS" />
<! -- Version support -->
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" /> <uses-sdk android:minSdkVersion="3" />
Now we have finished our work. You can add mapview to the layout file. The Code is as follows:
<com.baidu.mapapi.MapView android:id="@+id/mv_locate_map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" />
Write a class that inherits mapactivity and set the layout file to display it on the map, as shown below:
Now let's talk about the use of several main Baidu API classes. First, we need to get an instance of the bmapmanager object. We just saw that our layout file has not been added to the key, in fact, the key of Baidu map is the code added to the program as follows:
Manager = new bmapmanager (context); manager. init ("API-KEY put here", new mkgenerallistener () {@ overridepublic void ongetpermissionstate (INT error) {If (error = mkevent. error_permission_denied) {// permission error }}@ overridepublic void ongetnetworkstate (INT error) {// network error}); manager. start (); locationmanager = manager. getlocationmanager ();
After getting the bmapmanager object, we can get the locationmanager object. This object should be clear to everyone. You can use it to get the current location information and add a location listener. What is different from Google map is that when a location listener is added, the listener trigger interval is not set together. If you need to use the setnoitifyinternal (maxtime, mintime) method of mklocationmanager, the maximum interval between notifications of the first parameter, and the second parameter is the minimum interval. Register the listener. This is the requestlocationupdates (listener) method that calls mklocationmanager. By setting these methods, we can regularly obtain the current location information.
Next, we should display the current position on the map and add the mark. To implement these functions, we need the mapview object. Of course, it is very easy to get the mapview object, so we should not say much about it. After obtaining the mapview object, you need to set several common parameter codes as follows:
mapView.setBuiltInZoomControls(true); mapView.setDrawOverlayWhenZooming(true);
The first is to add a zoom button on the map, and the second is to re-paint the map tag when scaling. To control the map, we need to get the mapcontroller Object Code as follows:
control = this.mapView.getController();
control.setZoom(zoomLeve);
In this way, the map controller is obtained. The second is to set the default zoom level of the map. The integer parameter between 0 and 18 is obtained. Here is the set maximum zoom level. However, the overlay and itemizedoverlay objects must be used to add tags to a map. Of course, Baidu map API provides several built-in objects for us to use. Here I use the mylocationoverlay Object Code as follows:
MyLocationOverlay overlay = new MyLocationOverlay(context, mapView); mapView.getOverlays().add(overlay)
During mylocationoverlay initialization, two parameters are required: the context object and the map object. In this way, an icon indicating the current position can be obtained and added to the map. Shows the effect. Then add the code for Updating the map to the location listener. The Code is as follows:
@Override
Public void onlocationchanged (location) {If (location! = NULL) {geopoint point = new geopoint (INT) (latitude * 1e6), (INT) (long1_* 1e6); // mv_locate_map.getoverlays (). clear (); // clear all marked managers. addoverlay (overlay); // Add the tag manager. getcontrol (). animateto (point); // move the map to the current point mv_locate_map.invalidate (); // redraw the map
}}
The reason why I want to clear all the tags here is that I have added a map tag that will be updated every few seconds. Otherwise, there will be more and more tags on the map, and the performance will be affected in the future, this is also based on the Program Logic. You do not need to clear it every time. Baidu map API also provides many map tagging and query functions. You can refer to Baidu map API documentation for details.
Next, let's talk about the problems I encountered during use and my solutions. First, let's talk about the problems I encountered.
In fact, this problem is mainly caused by the fact that I want to add multiple location listeners, and each listener has different tasks and different closing times. This is a problem, for example, my colleague has added three listeners, but I only need to call removeupdates (listener); the method will remove all listeners. After several days, I finally came up with a solution that put the listener in a map <string, locationlistener> while adding the listener, then, you can extract the listener object from the map based on the name when you need to remove it. I don't understand why this happens. It may be a bug.
Another thing to note is that the bmapmanager object must be called after initialization. start (); Method to display the map normally, otherwise the map will not be displayed, and of course there is a manager corresponding to it. stop (); method, this stop method cannot be used in disorder, if you are not completely separated from Baidu API, it is best not to call, otherwise all the listeners in the map will be invalid.
You also need to call the START () method in the onresume () method. The Code is as follows:
@Overrideprotected void onResume() {if (manager != null) { manager.onStartMap();} super.onResume();}
If you think I'm not doing anything right, please give me some advice and have a lot of exchanges.