1.把百度地图定位API(下载地址:http://lbsyun.baidu.com/sdk/download?selected=location),里面的lib福祉到自己的项目中
2,进行相关配置(官方文档:http://developer.baidu.com/map/index.php?title=android-locsdk/guide/v5-0)
3.源码:
package com.zzw.baidumap;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
LocationClient mLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
// 得到LocationClient
mLocationClient = new LocationClient(this);
// 得到LocationListenner
MyLocationListenner mListenner = new MyLocationListenner();
// 设置LocationClient选项
LocationClientOption option = new LocationClientOption();
option.setIsNeedAddress(true);
option.setAddrType("all");
option.setCoorType("bd09ll");// 设置为百度的定位类型
mLocationClient.setLocOption(option);
// 注册LocationListener
mLocationClient.registerLocationListener(mListenner);
// 开始启动
mLocationClient.start();
}
@Override
protected void onDestroy() {
// 结束启动
mLocationClient.stop();
super.onDestroy();
}
// 具体操作
private class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
try {
// 当前设备位置所在的省
String province = location.getProvince();
Log.d("province", province);
// 当前设备位置所在的市:比如:成都市
String city = location.getCity();
Log.d("city", city);
// 具体地址:从国家到街号-->中国xx省xx市xx区xx路xx号-附x
String addeStr = location.getAddrStr();
Log.d("addeStr", addeStr);
// 国家
String country = location.getCountry();
Log.d("country", country);
// 区,比如:金牛区
String district = location.getDistrict();
Log.d("district", district);
// 街道 比如:西安南路
String street = location.getStreet();
Log.d("street", street);
// 具体的街道号数 比如:60号-附2
String streetNumber = location.getStreetNumber();
Log.d("streetNumber", streetNumber);
// 当前时间
String time = location.getTime();
Log.d("time", time);
Log.d("当前位置", addeStr);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}