百度地圖定位簽到功能,百度地圖簽到

來源:互聯網
上載者:User

百度地圖定位簽到功能,百度地圖簽到

1. 注意 key 一定要在activity 前面

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>

<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="6dsXhPU5WWHcBxGwQtHZKIGfZZBorMZ5" />
2  .xml 檔案
<com.baidu.mapapi.map.MapView
android:id="@+id/map_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

</com.baidu.mapapi.map.MapView>

3. java檔案
public class LoactionView extends AppCompatActivity {
MapView mMapView;
BaiduMap mBaiduMap;
LocationClient mLocationClient;
LocationClientOption mOption;
boolean isFirstLoc = true;// 是否首次定位
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_loaction_view);
mMapView = (MapView) findViewById(R.id.map_view);
mBaiduMap = mMapView.getMap();
mBaiduMap.setMyLocationEnabled(true);
mLocationClient = new LocationClient(getApplicationContext());
mLocationClient.setLocOption(getDefaultLocationClientOption());
mLocationClient.registerLocationListener(mBDLocationListener);
mLocationClient.start();

mMapView.refreshDrawableState();
}
/***
*
* @return DefaultLocationClientOption
*/
public LocationClientOption getDefaultLocationClientOption(){

mOption = new LocationClientOption();
mOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可選,預設高精度,設定定位元模式,高精度,低功耗,僅裝置
mOption.setCoorType("bd09ll");//可選,預設gcj02,設定返回的定位結果座標系,如果配合百度地圖使用,建議設定為bd09ll;
//mOption.setScanSpan(0);//可選,預設0,即僅定位一次,設定發起定位請求的間隔需要大於等於1000ms才是有效
mOption.setIsNeedAddress(true);//可選,設定是否需要地址資訊,預設不需要
mOption.setIsNeedLocationDescribe(true);//可選,設定是否需要地址描述
mOption.setNeedDeviceDirect(false);//可選,設定是否需要裝置方向結果
mOption.setLocationNotify(false);//可選,預設false,設定是否當gps有效時按照1S1次頻率輸出GPS結果
mOption.setIgnoreKillProcess(true);//可選,預設true,定位SDK內部是一個SERVICE,並放到了獨立進程,設定是否在stop的時候殺死這個進程,預設不殺死
mOption.setIsNeedLocationDescribe(true);//可選,預設false,設定是否需要位置語義化結果,可以在BDLocation.getLocationDescribe裡得到,結果類似於“在北京天安門附近”
mOption.setIsNeedLocationPoiList(true);//可選,預設false,設定是否需要POI結果,可以在BDLocation.getPoiList裡得到
mOption.SetIgnoreCacheException(false);//可選,預設false,設定是否收集CRASH資訊,預設收集

return mOption;
}
private BDLocationListener mBDLocationListener = new BDLocationListener() {

@Override
public void onReceiveLocation(BDLocation location) {
if (location == null || mMapView == null)
return;
MyLocationData locData = new MyLocationData.Builder().
accuracy(location.getRadius()).direction(100).latitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());

//地表徵圖注
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
OverlayOptions options = new MarkerOptions().position(ll).icon(bitmapDescriptor);
mBaiduMap.addOverlay(options);
/*
* 標繪圓
* */
CircleOptions circleOptions = new CircleOptions();
circleOptions.center(ll);//設定圓心座標
circleOptions.fillColor(0Xaafaa355);//圓的填充顏色
circleOptions.fillColor(0Xaafaa355);//圓的填充顏色
circleOptions.radius(400);//設定半徑
circleOptions.stroke(new Stroke(2, 0xAA00FF00));//設定邊框
mBaiduMap.addOverlay(circleOptions);

MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 16); //設定地圖中心點以及縮放層級
//MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}


// TODO Auto-generated method stub
if (null != location && location.getLocType() != BDLocation.TypeServerError) {
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
/**
* 時間也可以使用systemClock.elapsedRealtime()方法 擷取的是自從開機以來,每次回調的時間;
* location.getTime() 是指服務端出本次結果的時間,如果位置不發生變化,則時間不變
*/
sb.append(location.getTime());
sb.append("\nerror code : ");
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());
sb.append("\nCountryCode : ");
sb.append(location.getCountryCode());
sb.append("\nCountry : ");
sb.append(location.getCountry());
sb.append("\ncitycode : ");
sb.append(location.getCityCode());
sb.append("\ncity : ");
sb.append(location.getCity());
sb.append("\nDistrict : ");
sb.append(location.getDistrict());
sb.append("\nStreet : ");
sb.append(location.getStreet());
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append("\nDescribe: ");
sb.append(location.getLocationDescribe());
sb.append("\nDirection(not all devices have value): ");
sb.append(location.getDirection());
sb.append("\nPoi: ");
if (location.getPoiList() != null && !location.getPoiList().isEmpty()) {
for (int i = 0; i < location.getPoiList().size(); i++) {
Poi poi = (Poi) location.getPoiList().get(i);
sb.append(poi.getName() + ";");
}
}
if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位結果
sb.append("\nspeed : ");
sb.append(location.getSpeed());// 單位:km/h
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
sb.append("\nheight : ");
sb.append(location.getAltitude());// 單位:米
sb.append("\ndescribe : ");
sb.append("gps定位成功");
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 網路定位結果
// 電訊廠商資訊
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號和大體定位時間到loc-bugs@baidu.com,會有人追查原因");
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ");
sb.append("網路不同導致定位失敗,請檢查網路是否通暢");
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
sb.append("\ndescribe : ");
sb.append("無法擷取有效定位依據導致定位失敗,一般是由於手機的原因,處于飛行模式下一般會造成這種結果,可以試著重啟手機");
}
logMsg(sb.toString());
}
}

};
/**
* 顯示請求字串
*
* @param str
*/
public void logMsg(String str) {
Log.e("msg", str);
/* try {
if (LocationResult != null)
LocationResult.setText(str);
} catch (Exception e) {
e.printStackTrace();
}*/
}

// 三個狀態實現地圖生命週期管理
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}

@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}

@Override
protected void onDestroy() {
mLocationClient.unRegisterLocationListener(mBDLocationListener);
mLocationClient.stop();
mBaiduMap.setMyLocationEnabled(false);
super.onDestroy();
mMapView.onDestroy();
mMapView = null;
}
}
4.目標地址和我的定位地址,兩個點之間的距離,如何計算
http://blog.csdn.net/mad1989/article/details/9933089





相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.