1.佈局
<com.baidu.mapapi.map.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"android:layout_height="match_parent"
android:clickable="true" />
<TextView
android:id="@+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="位置:你去哪兒了"
android:layout_centerInParent="true"/>
2.代碼
配置://在清單文件註冊 android:name=".app.MyApp"
<service android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote">
</service>
public class MyApp extends Application{
@Override
public void onCreate() {
super.onCreate();
SDKInitializer.initialize(getApplicationContext()); }}
activity:
public class LocationActivity extends Activity {
private MapView mapView;
private TextView tv_location;
public LocationClientmLocationClient = null;
public BDLocationListenermyListener = new MyLocationListener();
private BaiduMap baiduMap;
private boolean isFirstLoc =true;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
//找控件
mapView = (MapView)findViewById(R.id.mapview);
//獲取地圖
baiduMap = mapView.getMap();
//開啓定位圖層
baiduMap.setMyLocationEnabled(true);
tv_location = (TextView)findViewById(R.id.tv_location);
//初始化定位對象 聲明LocationClient類
mLocationClient = newLocationClient(getApplicationContext());
//註冊監聽函數
mLocationClient.registerLocationListener(myListener);
initLocation();
mLocationClient.start();
}
private void initLocation() {
LocationClientOption option =new LocationClientOption();
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
//可選,默認高精度,設置定位模式,高精度,低功耗,僅設備
option.setCoorType("bd09ll");
//可選,默認gcj02,設置返回的定位結果座標系
int span = 1000;
option.setScanSpan(span);
//可選,默認0,即僅定位一次,設置發起定位請求的間隔需要大於等於1000ms纔是有效的
option.setIsNeedAddress(true);
//可選,設置是否需要地址信息,默認不需要
option.setOpenGps(true);
//可選,默認false,設置是否使用gps
option.setLocationNotify(true);
//可選,默認false,設置是否當GPS有效時按照1S/1次頻率輸出GPS結果
option.setIsNeedLocationDescribe(true);
//可選,默認false,設置是否需要位置語義化結果,可以在BDLocation.getLocationDescribe裏得到,結果類似於“在北京天安門附近”
option.setIsNeedLocationPoiList(true);
//可選,默認false,設置是否需要POI結果,可以在BDLocation.getPoiList裏得到
option.setIgnoreKillProcess(false);
//可選,默認true,定位SDK內部是一個SERVICE,並放到了獨立進程,設置是否在stop的時候殺死這個進程,默認不殺死
option.SetIgnoreCacheException(false);
//可選,默認false,設置是否收集CRASH信息,默認收集
option.setEnableSimulateGps(false);
//可選,默認false,設置是否需要過濾GPS仿真結果,默認需要
mLocationClient.setLocOption(option);
}
public class MyLocationListenerimplements BDLocationListener {
@Override
public voidonReceiveLocation(BDLocation location) {
//獲取定位結果
StringBuffer sb = newStringBuffer(256);
sb.append("time :");
sb.append(location.getTime()); //獲取定位時間
sb.append("\nerrorcode : ");
sb.append(location.getLocType()); //獲取類型類型
sb.append("\nlatitude : ");
sb.append(location.getLatitude()); //獲取緯度信息
sb.append("\nlontitude :");
sb.append(location.getLongitude()); //獲取經度信息
sb.append("\nradius: ");
sb.append(location.getRadius()); //獲取定位精準度
//---------------------將地圖跟定位關聯起來----------------------------------------
MyLocationData locData = newMyLocationData.Builder()
.accuracy(location.getRadius())
// 此處設置開發者獲取到的方向信息,順時針0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
// 設置定位數據
baiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = newLatLng(location.getLatitude(),
location.getLongitude());
MapStatus.Builderbuilder = new MapStatus.Builder();
builder.target(ll).zoom(18.0f);
baiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}
//---------------------將地圖跟定位關聯起來----------------------------------------
if (location.getLocType()== BDLocation.TypeGpsLocation) {
// GPS定位結果
sb.append("\nspeed : ");
sb.append(location.getSpeed()); // 單位:公里每小時
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber()); //獲取衛星數
sb.append("\nheight : ");
sb.append(location.getAltitude()); //獲取海拔高度信息,單位米
sb.append("\ndirection : ");
sb.append(location.getDirection()); //獲取方向信息,單位度
sb.append("\naddr : ");
sb.append(location.getAddrStr()); //獲取地址信息
sb.append("\ndescribe : ");
sb.append("gps定位成功");
} else if (location.getLocType() ==BDLocation.TypeNetWorkLocation) {
// 網絡定位結果
sb.append("\naddr : ");
sb.append(location.getAddrStr()); //獲取地址信息
sb.append("\noperationers : ");
sb.append(location.getOperators()); //獲取運營商信息
sb.append("\ndescribe : ");
sb.append("網絡定位成功");
} else if(location.getLocType() == BDLocation.TypeOffLineLocation) {
// 離線定位結果
sb.append("\ndescribe : ");
sb.append("離線定位成功,離線定位結果也是有效的");
} else if(location.getLocType() == BDLocation.TypeServerError) {
sb.append("\ndescribe : ");
sb.append("服務端網絡定位失敗,可以反饋IMEI號和大體定位時間到[email protected],會有人追查原因");
} else if(location.getLocType() == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ");
sb.append("網絡不同導致定位失敗,請檢查網絡是否通暢");
} else if(location.getLocType() == BDLocation.TypeCriteriaException) {
sb.append("\ndescribe : ");
sb.append("無法獲取有效定位依據導致定位失敗,一般是由於手機的原因,處於飛行模式下一般會造成這種結果,可以試着重啓手機");
}
sb.append("\nlocationdescribe : ");
sb.append(location.getLocationDescribe()); //位置語義化信息
final List<Poi>list = location.getPoiList(); // POI數據
if (list != null) {
sb.append("\npoilist size = : ");
sb.append(list.size());
for (Poi p : list) {
sb.append("\npoi= : ");
sb.append(p.getId()+ " " + p.getName() + " " + p.getRank());
}
}
//此方法運行在子線程
runOnUiThread(newRunnable() {
@Override
public void run() {
tv_location.setText(list.get(2).getName());
}
});
Log.i("BaiduLocationApiDem", sb.toString() + ",目前位置:" +list.get(2).getName());
}
@Override
public voidonConnectHotSpotMessage(String s, int i) {
}
}
}