Android Baidu map positioning development, android Baidu Map
I. activity
Import android. app. Activity;
Import android. graphics. Point;
Import android. graphics. PointF;
Import android. OS. Bundle;
Import android. OS. Message;
Import com. baidu. location. BDLocation;
Import com. baidu. location. BDLocationListener;
Import com. baidu. location. LocationClient;
Import com. baidu. location. LocationClientOption;
Import com. baidu. mapapi. map. BaiduMap;
Import com. baidu. mapapi. map. BaiduMapOptions;
Import com. baidu. mapapi. map. BitmapDescriptor;
Import com. baidu. mapapi. map. MapStatus;
Import com. baidu. mapapi. map. MapStatusUpdate;
Import com. baidu. mapapi. map. MapStatusUpdateFactory;
Import com. baidu. mapapi. map. MapView;
Import com. baidu. mapapi. map. MyLocationConfiguration;
Import com. baidu. mapapi. map. MyLocationConfiguration. LocationMode;
Import com. baidu. mapapi. map. MyLocationData;
Import com. baidu. mapapi. map. Overlay;
Import com. baidu. mapapi. model. LatLng;
/**
* This demo is used to demonstrate how to use the positioning SDK to implement positioning, and use MyLocationOverlay to draw positioning positions. It also shows how to use custom icons to draw and pop up bubbles when you click
*
*/
Public class LocationDemo extends Activity {
// Positioning
LocationClient mLocClient; // --------------- first statement
MapView mMapView;
BaiduMap mBaiduMap;
Private LocationMode mCurrentMode;
Public MyLocationListenner myListener = new MyLocationListenner ();
MyLocationData locData;
BitmapDescriptor mCurrentMarker;
Boolean isFirstLoc = true; // whether to locate the object for the first time
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_location );
// RequestLocButton. setText ("normal ");
MCurrentMode = LocationMode. FOLLOWING;
// 1 Map Initialization
MMapView = (MapView) findViewById (R. id. bmapView); // longitude 120.219375, latitude 30.259244 Hangzhou
// MMapView = new MapView (this );
// MMapView. refreshDrawableState ();
System. out. println ("initialization successful, haha! ");
MBaiduMap = mMapView. getMap ();
// 2 enable the positioning Layer
MBaiduMap. setMyLocationEnabled (true );
MBaiduMap. setMyLocationConfigeration (new MyLocationConfiguration (
MCurrentMode, true, mCurrentMarker ));
// 3 locate Initialization
MLocClient = new LocationClient (this );
MLocClient. registerLocationListener (myListener); // --------------- first register the location listener
LocationClientOption option = new LocationClientOption ();
Option. setOpenGps (true); // enable gps
Option. setLocationMode (LocationClientOption. LocationMode. Hight_Accuracy );
Option. setCoorType ("bd09ll"); // sets the coordinate type.
Option. setScanSpan (3000 );
Option. setIsNeedAddress (true );
MLocClient. setLocOption (option); // --------------- first sets locationoption
MLocClient. start (); // --------------- first start sdk Positioning
MLocClient. requestLocation ();
}
/**
* Locate the SDK listener Function
*/
Public class MyLocationListenner implements BDLocationListener {
@ Override
Public void onReceiveLocation (BDLocation location ){
// After map view is destroyed, it is not processed in the new receiving location
If (location = null | mMapView = null)
Return;
LocData = new MyLocationData. Builder ()
. Accuracy (location. getRadius ())
// Set the direction information obtained by the developer, Which is 0-360 clockwise.
. Direction (location. getDirection (). latitude (location. getLatitude ())
. Longpolling (location. getlongpolling (). build ();
System. out. println ("location. lat "+ location. getLatitude () + "&" + "location. long "+ location. getlongpolling ());
MBaiduMap. setMyLocationEnabled (true); // ------ 1 allows layer locating
MBaiduMap. setMyLocationData (locData); // ------ 2 allow the layer to be located and then set the location data
LatLng ll = new LatLng (location. getLatitude (),
Location. getlongpolling ());
MapStatusUpdate u = MapStatusUpdateFactory. newLatLngZoom (ll, 19 );
MBaiduMap. animateMapStatus (u );
If (isFirstLoc ){
IsFirstLoc = false;
// Set the map zoom ratio to 17 Levels and 100 meters
MapStatusUpdate MS = MapStatusUpdateFactory. newLatLngZoom (ll, 19 );
System. out. println ("33333333333333333 ");
MBaiduMap. setMapStatus (MS );
}
}
}
@ Override
Protected void onPause (){
MMapView. onPause ();
Super. onPause ();
}
@ Override
Protected void onResume (){
MMapView. onResume ();
Super. onResume ();
}
@ Override
Protected void onDestroy (){
MLocClient. unRegisterLocationListener (myListener );
// Destroy location upon exit
MLocClient. stop ();
// Close the positioning Layer
MBaiduMap. setMyLocationEnabled (false );
MMapView. onDestroy ();
MMapView = null;
Super. onDestroy ();
}
}
Ii. layout. xml (activity_location.xml)
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<Com. baidu. mapapi. map. MapView
Android: id = "@ + id/bmapView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: clickable = "true"/>
<LinearLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: layout_marginTop = "80dip"
Android: background = "# D000"
Android: minWidth = "100dip"
Android: orientation = "vertical"
Android: padding = "2dp">
<RadioGroup
Android: id = "@ + id/radioGroup"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: contentDescription = "positioning icon">
<RadioButton
Android: id = "@ + id/defaulticon"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: checked = "true"
Android: text = "Default icon">
</RadioButton>
<RadioButton
Android: id = "@ + id/customicon"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Custom icon">
</RadioButton>
</RadioGroup>
</LinearLayout>
<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: layout_alignParentTop = "true"
Android: layout_marginRight = "25dp"
Android: layout_marginTop = "10dip"/>
</RelativeLayout>
Finally, do not forget to add this class to the configuration file.