百度地圖定位簽到功能

来源:http://www.cnblogs.com/520-1314/archive/2016/06/01/5550139.html
-Advertisement-
Play Games

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


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號和大體定位時間到[email protected],會有人追查原因");
} 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






您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 詳細的圖文可以到我的百度經驗去查看:http://jingyan.baidu.com/article/cd4c2979eda109756e6e60de.html 首先是註冊頁面的佈局: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2 ...
  • 本文主要介紹android中如何在外部存儲讀寫數據 sd卡的路徑 sdcard:2.3之前的sd卡路徑 mnt/sdcard:4.3之前的sd卡路徑 storage/sdcard:4.3之後的sd卡路徑 打開file explorer 可以看到sdcard是個空的文件夾,因為這個文件夾是個快捷方式, ...
  • 摘自知乎http://www.zhihu.com/question/20326729 來源:知乎著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。 首先如果你小團隊或者個人開發,當然可以用Xcode,如果大公司跨部門審核調整各種,還是有個快速的原型。 如你要求,可交互。包括常用的A ...
  • 效果圖 1. 導入 .aar 2. fragment_line_chart.xml ...
  • 最近看視頻瞭解了一下應用程式的啟動原理,這裡就做一個博客和大家分享一下,相互討論,如果有什麼補充或不同的意見可以提出來! 1、程式入口 眾所周知,一個應用程式的入口一般是一個 main 函數,iOS也不例外,在工程的 Supporting Files 文件夾中你可以找到main.m,他就是程式的入口 ...
  • cordova plugin add org.apache.cordova.vibration cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications cordova plugin add cor ...
  • MVP是從MVC演變過來的。M即Model層負責提供數據,V即View層負責顯示,P指Presenter層負責邏輯處理。在MVP中View不直接使用Model,它們通過Presenter來通信,它們之間的交互都在Presenter內部進行。實現了View和Model的耦合。<! more 在MVP中 ...
  • 當原生控制項不符合需求,並且進行拓展或組合也無濟於事的時候,就需要自己來繪製一個控制項了。<! more 全新的View 通過源碼,我們可以看到所有的控制項都是通過繼承View類來實現的。所以,我們要創建全新的控制項,也是要通過繼承View來實現。 案例 案例同樣來自《Android群英傳》。 繪製圓弧文本 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...