1. Copy the Libs from the Baidu Map location API (: http://lbsyun.baidu.com/sdk/download?selected=location) to your project Libs
2. Make the relevant configuration, see the Official document: http://developer.baidu.com/map/index.php?title=android-locsdk/guide/v5-0
3. Code:
1 PackageCom.zzw.baidumap;2 3 Importcom.baidu.location.BDLocation;4 ImportCom.baidu.location.BDLocationListener;5 Importcom.baidu.location.LocationClient;6 Importcom.baidu.location.LocationClientOption;7 8 Importandroid.app.Activity;9 ImportAndroid.os.Bundle;Ten ImportAndroid.util.Log; One A Public classMainactivityextendsActivity { - locationclient mlocationclient; - the @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - //Setcontentview (r.layout.activity_main); + - //Get Locationclient +Mlocationclient =NewLocationclient ( This); A at //Get Locationlistenner -Mylocationlistenner Mlistenner =NewMylocationlistenner (); - - //Set locationclient options -locationclientoption option =Newlocationclientoption (); -Option.setisneedaddress (true); inOption.setaddrtype ("All"); -Option.setcoortype ("Bd09ll");//set to Baidu's positioning type to + mlocationclient.setlocoption (option); - the //Register Locationlistener * Mlocationclient.registerlocationlistener (mlistenner); $ Panax Notoginseng //start Start - Mlocationclient.start (); the } + A @Override the protected voidOnDestroy () { + //End Startup - mlocationclient.stop (); $ Super. OnDestroy (); $ } - - //Specific Operation the Private classMylocationlistennerImplementsBdlocationlistener { - Wuyi @Override the Public voidonreceivelocation (bdlocation location) { - Try { Wu //the province where the current device location is located -String province =location.getprovince (); AboutLOG.D ("province", province); $ - //the city where the current device is located: For example, Chengdu -String City =location.getcity (); -LOG.D ("City", city); A + //specific address: from the country to the street--xx xx province, China xx xx Road XX No.-attached x theString Addestr =location.getaddrstr (); -LOG.D ("Addestr", addestr); $ the //National theString Country =location.getcountry (); theLOG.D ("Country", country); the - //areas, such as: Jinniu District inString district =location.getdistrict (); theLOG.D ("District", district); the About //streets such as: South Road theString Street =Location.getstreet (); theLOG.D ("Street", street); the + //specific street numbers such as: 60th-attached 2 -String Streetnumber =Location.getstreetnumber (); theLOG.D ("Streetnumber", streetnumber);Bayi the //Current Time theString time =location.gettime (); -LOG.D ("Time", time); - theLOG.D ("Current Position", addestr); the}Catch(Exception e) { the e.printstacktrace (); the } - the } the the }94}
Baidu Map API Positioning--third party open source--Baidu map (a)