Baidu Map Location

Source: Internet
Author: User

Package Com.example.maplianx;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

Import com.baidu.location.BDLocation;
Import Com.baidu.location.BDLocationListener;
Import com.baidu.location.LocationClient;
Import com.baidu.location.LocationClientOption;
Import Com.baidu.mapapi.SDKInitializer;
Import Com.baidu.mapapi.map.BaiduMap;
Import Com.baidu.mapapi.map.BitmapDescriptor;
Import Com.baidu.mapapi.map.BitmapDescriptorFactory;
Import Com.baidu.mapapi.map.MapStatusUpdate;
Import Com.baidu.mapapi.map.MapStatusUpdateFactory;
Import Com.baidu.mapapi.map.MapView;
Import com.baidu.mapapi.map.MarkerOptions;
Import Com.baidu.mapapi.map.MyLocationData;
Import com.baidu.mapapi.map.OverlayOptions;
Import Com.baidu.mapapi.model.LatLng;

public class Mainactivity extends Activity implements Onclicklistener {

Mapview Mmapview = null;
Private Button BT1,BT2,BT3;
Baidumap Mbaidumap;
Bitmapdescriptor BdB;
Bitmapdescriptor BdC;
Positioning
Locating related claims
Public locationclient locationclient = null;
Custom icons
Bitmapdescriptor mcurrentmarker = null;
Boolean isfirstloc = true;//is first positioned
Public Bdlocationlistener MyListener = new Bdlocationlistener () {
@Override
public void Onreceivelocation (bdlocation location) {
Map view is not processed in the new receive location after it is destroyed
if (location = = NULL | | mmapview = = NULL)
Return

Mylocationdata locdata = new Mylocationdata.builder ()
. Accuracy (Location.getradius ())
Here you set the direction information the developer gets, clockwise 0-360
. direction (+). Latitude (Location.getlatitude ())
. Longitude (Location.getlongitude ()). build ();
Mbaidumap.setmylocationdata (Locdata); Setting location data
if (Isfirstloc) {
Isfirstloc = false;
LATLNG ll = new Latlng (Location.getlatitude (),
Location.getlongitude ());
Mapstatusupdate U = mapstatusupdatefactory.newlatlngzoom (ll, 16); Set the map center point and zoom level
Mbaidumap.animatemapstatus (U);
}
}
};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Sdkinitializer.initialize (Getapplicationcontext ());
Setcontentview (R.layout.activity_main);

Get map Control Reference
Mmapview = (Mapview) Findviewbyid (R.id.bmapview);

BT1 = (Button) Findviewbyid (R.id.button1);
BT2 = (Button) Findviewbyid (R.id.button2);
BT3 = (Button) Findviewbyid (R.id.button3);

Bt1.setonclicklistener (this);
Bt2.setonclicklistener (this);
Bt3.setonclicklistener (this);

Mbaidumap = Mmapview.getmap ();
Defining maker coordinate points
LATLNG point = new Latlng (39.963175, 116.400244);
Building the marker icon
Bitmapdescriptor bitmap = Bitmapdescriptorfactory.fromresource (R.drawable.icon_marka);
Build markeroption to add marker on the map
overlayoptions option = new Markeroptions (). Position (point). icon (bitmap);
Add marker on the map and show
Mbaidumap.addoverlay (option);

Turn on positioning layers
Mbaidumap.setmylocationenabled (TRUE);
Locationclient = new Locationclient (Getapplicationcontext ()); Instantiate the Locationclient class
Locationclient.registerlocationlistener (MyListener); Registering the Listener function
This.setlocationoption (); Set positioning parameters
Locationclient.start (); Start positioning
}

@Override
protected void OnDestroy () {
Super.ondestroy ();
Implement Mmapview.ondestroy () when activity executes OnDestroy to achieve map life cycle management
Locationclient.stop ();
Mbaidumap.setmylocationenabled (FALSE);
Mmapview.ondestroy ();
Mmapview = null;
}
@Override
protected void Onresume () {
Super.onresume ();
Executes Mmapview when the activity executes Onresume. Onresume () for map life cycle management
Mmapview.onresume ();
}
@Override
protected void OnPause () {
Super.onpause ();
Executes Mmapview when the activity executes OnPause. OnPause () for map life cycle management
Mmapview.onpause ();
}

@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Switch (Arg0.getid ()) {
Case R.ID.BUTTON1://General
Mbaidumap.setmaptype (Baidumap.map_type_normal);
Break

Case r.id.button2://Satellite
Mbaidumap.setmaptype (Baidumap.map_type_satellite);
Break

Case r.id.button3://Map
Mbaidumap.settrafficenabled (TRUE);
Break

Default
Break
}
}
/**
* Set Positioning parameters
*/
@SuppressWarnings ("deprecation")
private void Setlocationoption () {
locationclientoption option = new Locationclientoption ();
Option.setopengps (TRUE); Turn on GPS
Option.setlocationmode (LocationClientOption.LocationMode.Hight_Accuracy);//Set positioning mode
Option.setcoortype ("Bd09ll"); The returned positioning result is Baidu latitude and longitude, the default value gcj02
Option.setscanspan (5000); Set the time interval for initiating a location request to 5000ms
Option.setisneedaddress (TRUE); The returned positioning result contains address information
Option.setneeddevicedirect (TRUE); The returned positioning result contains the direction of the phone head
Option.setpriority (Locationclientoption.networkfirst);//Set Positioning priority
Option.setisneedaddress (TRUE);
locationclient.setlocoption (option);
}
}

Xml

<com.baidu.mapapi.map.mapview
Android:id= "@+id/bmapview"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:clickable= "true"
android:layout_weight= "1"
/>

Baidu Map Location

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.