First, 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 show how to use the positioning SDK to achieve positioning, and use Mylocationoverlay to draw positioning location while showing how to draw with a custom icon and click on the pop-up bubble
*
*/
public class Locationdemo extends Activity {
Positioning related
Locationclient mlocclient; ---------------First Statement
Mapview Mmapview;
Baidumap Mbaidumap;
Private Locationmode Mcurrentmode;
Public Mylocationlistenner MyListener = new Mylocationlistenner ();
Mylocationdata Locdata;
Bitmapdescriptor Mcurrentmarker;
Boolean isfirstloc = true;//is first positioned
@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 ("initialized successfully, haha!") ");
Mbaidumap = Mmapview.getmap ();
//2 to turn on the positioning Layer
mbaidumap.setmylocationenabled (true);
Mbaidumap.setmylocationconfigeration (New Mylocationconfiguration (
Mcurrentmode, True, Mcurrentmarker));
3 positioning initialization
Mlocclient = new Locationclient (this);
Mlocclient.registerlocationlistener (MyListener); ---------------First registration Location monitoring
locationclientoption option = new Locationclientoption ();
Option.setopengps (TRUE);//Turn on GPS
Option.setlocationmode (LocationClientOption.LocationMode.Hight_Accuracy);
Option.setcoortype ("Bd09ll"); Set the coordinate type
Option.setscanspan (3000);
Option.setisneedaddress (TRUE);
mlocclient.setlocoption (option); ---------------First Settings Locationoption
Mlocclient.start (); ---------------first start SDK targeting
Mlocclient.requestlocation ();
}
/**
* Locate the SDK listener function
*/
public class Mylocationlistenner implements Bdlocationlistener {
@Override
public void onreceivelocation (bdlocation location) {
//Map view is not processed in the new received position after destruction
if (locations = = null | | Mmapview = = null)
return;
Locdata= new Mylocationdata.builder ()
. Accuracy (Location.getradius ())
//Here Set the direction information the developer gets, clockwise 0-360
. Direction (Location.getdirection ()). Latitude (Location.getlatitude ())
. Longitude (Location.getlongitude ()). Build ();
System.out.println ("Location.lat" +location.getlatitude () + "&&&&&&&&" + " Location.long "+location.getlongitude ());
Mbaidumap.setmylocationenabled (TRUE); ------1 Allow positioning layers
Mbaidumap.setmylocationdata (Locdata); ------2 allows you to set positioning data after positioning the layer
LATLNG ll = new Latlng (Location.getlatitude (),
Location.getlongitude ());
Mapstatusupdate u = mapstatusupdatefactory.newlatlngzoom (ll,19);
Mbaidumap.animatemapstatus (U);
if (Isfirstloc) {
Isfirstloc = false;
Set Map scale: 17-level 100 m
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 when exiting
Mlocclient.stop ();
Turn off positioning layers
Mbaidumap.setmylocationenabled (FALSE);
Mmapview.ondestroy ();
Mmapview = null;
Super.ondestroy ();
}
}
Second, 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, in the configuration file, do not forget to add this class.
Android Baidu map positioning development