2016基於百度地圖定位
現在百度改版後有點不習慣,沒辦法,只有自己又去看了一下,比以前的簡單方便多,有時間我會把導航等給做出來,當然了這個程式碼只是對於初學者來的,因為我自己也是初學者,你自己也可以在百度API上去看,試著寫一遍,我這個主要的目的還是因為最近需要我才弄了一個定位的東西提出來,方便我自己以後又要去下載百度的東西
還需要這個包
private LocationService locationService;
private TextView LocationResult;
// private Button startLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// -----------demo view config ------------
setContentView(R.layout.activity_main);
LocationResult = (TextView) findViewById(R.id.textview1);
LocationResult.setMovementMethod(ScrollingMovementMethod.getInstance());
}
/**
* 顯示請求字串
*
* @param str
*/
public void logMsg(String str) {
try {
if (LocationResult != null)
LocationResult.setText(str);
} catch (Exception e) {
e.printStackTrace();
}
}
/***
* Stop location service
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
locationService.unregisterListener(mListener); //登出掉監聽
locationService.stop(); //停止定位服務
super.onStop();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
// -----------location config ------------
locationService = ((LocationApplication) getApplication()).locationService;
//獲取locationservice例項,建議應用中只初始化1個location例項,然後使用,可以參考其他示例的activity,都是通過此種方式獲取locationservice例項的
locationService.registerListener(mListener);
//註冊監聽
int type = getIntent().getIntExtra("from", 0);
if (type == 0) {
locationService.setLocationOption(locationService.getDefaultLocationClientOption());
} else if (type == 1) {
locationService.setLocationOption(locationService.getOption());
}
// if (startLocation.getText().toString().equals(getString(R.string.startlocation))) {
locationService.start();// 定位SDK
// start之後會預設發起一次定位請求,開發者無須判斷isstart並主動呼叫request
// startLocation.setText(getString(R.string.stoplocation));
// } else {
locationService.stop();
// startLocation.setText(getString(R.string.startlocation));
// }
// startLocation.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// if (startLocation.getText().toString().equals(getString(R.string.startlocation))) {
// locationService.start();// 定位SDK
// // start之後會預設發起一次定位請求,開發者無須判斷isstart並主動呼叫request
// startLocation.setText(getString(R.string.stoplocation));
// } else {
// locationService.stop();
// startLocation.setText(getString(R.string.startlocation));
// }
// }
// });
}
/*****
* @see copy funtion to you project
* 定位結果回撥,重寫onReceiveLocation方法,可以直接拷貝如下程式碼到自己工程中修改
*
*/
private BDLocationListener mListener = new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
// 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("\nlocType : ");// 定位型別
// sb.append(location.getLocType());
// sb.append("\nlocType description : ");// *****對應的定位型別說明*****
// sb.append(location.getLocTypeDescription());
// sb.append("\nlatitude : ");// 緯度
// sb.append(location.getLatitude());
// sb.append("\nlontitude : ");// 經度
// sb.append(location.getLongitude());
// 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("\nUserIndoorState: ");// *****返回使用者室內外判斷結果*****
// sb.append(location.getUserIndoorState());
// sb.append("\nDirection(not all devices have value): ");
// sb.append(location.getDirection());// 方向
// sb.append("\nlocationdescribe: ");
// sb.append(location.getLocationDescribe());// 位置語義化資訊
// sb.append("\nPoi: ");// POI資訊
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("\ngps status : ");
sb.append(location.getGpsAccuracyStatus());// *****gps質量判斷*****
sb.append("\ndescribe : ");
sb.append("gps定位成功");
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 網路定位結果
// 運營商資訊
if (location.hasAltitude()) {// *****如果有海拔高度*****
sb.append("\nheight : ");
sb.append(location.getAltitude());// 單位:米
}
// 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());
}
}
};
//service服務
/.................華麗的分割線........................../
private LocationClient client = null;
private LocationClientOption mOption,DIYoption;
private Object objLock = new Object();
/***
*
* @param locationContext
*/
public LocationService(Context locationContext){
synchronized (objLock) {
if(client == null){
client = new LocationClient(locationContext);
client.setLocOption(getDefaultLocationClientOption());
}
}
}
/***
*
* @param listener
* @return
*/
public boolean registerListener(BDLocationListener listener){
boolean isSuccess = false;
if(listener != null){
client.registerLocationListener(listener);
isSuccess = true;
}
return isSuccess;
}
public void unregisterListener(BDLocationListener listener){
if(listener != null){
client.unRegisterLocationListener(listener);
}
}
/***
*
* @param option
* @return isSuccessSetOption
*/
public boolean setLocationOption(LocationClientOption option){
boolean isSuccess = false;
if(option != null){
if(client.isStarted())
client.stop();
DIYoption = option;
client.setLocOption(option);
}
return isSuccess;
}
public LocationClientOption getOption(){
return DIYoption;
}
/***
*
* @return DefaultLocationClientOption
*/
public LocationClientOption getDefaultLocationClientOption(){
if(mOption == null){
mOption = new LocationClientOption();
mOption.setLocationMode(LocationMode.Hight_Accuracy);//可選,預設高精度,設定定位模式,高精度,低功耗,僅裝置
mOption.setCoorType("bd09ll");//可選,預設gcj02,設定返回的定位結果座標系,如果配合百度地圖使用,建議設定為bd09ll;
mOption.setScanSpan(3000);//可選,預設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資訊,預設收集
mOption.setIsNeedAltitude(false);//可選,預設false,設定定位時是否需要海拔資訊,預設不需要,除基礎定位版本都可用
}
return mOption;
}
public void start(){
synchronized (objLock) {
if(client != null && !client.isStarted()){
client.start();
}
}
}
public void stop(){
synchronized (objLock) {
if(client != null && client.isStarted()){
client.stop();
}
}
}
//建立一個Util類放入一下程式碼
public final static String CoorType_GCJ02 = "gcj02";
public final static String CoorType_BD09LL= "bd09ll";
public final static String CoorType_BD09MC= "bd09";
/***
*61 : GPS定位結果,GPS定位成功。
*62 : 無法獲取有效定位依據,定位失敗,請檢查運營商網路或者wifi網路是否正常開啟,嘗試重新請求定位。
*63 : 網路異常,沒有成功向伺服器發起請求,請確認當前測試手機網路是否通暢,嘗試重新請求定位。
*65 : 定位快取的結果。
*66 : 離線定位結果。通過requestOfflineLocaiton呼叫時對應的返回結果。
*67 : 離線定位失敗。通過requestOfflineLocaiton呼叫時對應的返回結果。
*68 : 網路連線失敗時,查詢本地離線定位時對應的返回結果。
*161: 網路定位結果,網路定位定位成功。
*162: 請求串密文解析失敗。
*167: 服務端定位失敗,請您檢查是否禁用獲取位置資訊許可權,嘗試重新請求定位。
*502: key引數錯誤,請按照說明文件重新申請KEY。
*505: key不存在或者非法,請按照說明文件重新申請KEY。
*601: key服務被開發者自己禁用,請按照說明文件重新申請KEY。
*602: key mcode不匹配,您的ak配置過程中安全碼設定有問題,請確保:sha1正確,“;”分號是英文狀態;且包名是您當前執行應用的包名,請按照說明文件重新申請KEY。
*501~700:key驗證失敗,請按照說明文件重新申請KEY。
*/
public static float[] EARTH_WEIGHT = {0.1f,0.2f,0.4f,0.6f,0.8f}; // 推算計算權重_地球
//public static float[] MOON_WEIGHT = {0.0167f,0.033f,0.067f,0.1f,0.133f};
//public static float[] MARS_WEIGHT = {0.034f,0.068f,0.152f,0.228f,0.304f};
//需要用到的許可權
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_LOGS" >
</uses-permission>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
//配置你的Application
<application
android:allowBackup="true"
android:name="com.example.mathname.LocationApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
<intent-filter>
<action android:name="com.baidu.location.service_v2.2" >
</action>
</intent-filter>
</service>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="你自己的KEY" />
相關推薦
2016基於百度地圖定位
現在百度改版後有點不習慣,沒辦法,只有自己又去看了一下,比以前的簡單方便多,有時間我會把導航等給做出來,當然了這個程式碼只是對於初學者來的,因為我自己也是初學者,你自己也可以在百度API上去看,試著寫一遍,我這個主要的目的還是因為最近需要我才弄了一個定位的東西提
Android定位&地圖&導航——基於百度地圖移動獲取位置和自動定位
public class MainActivity extends Activity { public MyApplication app; static MapView mMapView = null; public MKMapViewListener mMapListen
Android定位&地圖&導航——基於百度地圖實現的定位功能
public class MyApplication extends Application{ public LocationClient mLocationClient = null; public GeofenceClient mGeofenceClient; publi
Android定位&地圖&導航——基於百度地圖,實現自定義圖示繪製並點選時彈出泡泡
public class MainActivity extends Activity { private EditText txtAddr; // 定位相關 LocationClient mLocClient; LocationData locData = nul
【MUI】百度地圖定位功能
scom 需要 read rto code lan containe convert marker 博主最近進行一款APP開發,需要用到定位功能,經過一番折騰,終於搞定,不廢話,代碼如下 mui.plusReady(function() {
基於百度地圖SDK和Elasticsearch GEO查詢的地理圍欄分析系統(3)-前端實現
方便 復制 類型 復制代碼 自動跳轉 rar 窗口 stack delete 轉載自:http://www.cnblogs.com/Auyuer/p/8086975.html MoonLight可視化訂單需求區域分析系統實現功能: 在現實生活中,計算機和互聯網迅速發展,
微信企業號開發:微信考勤百度地圖定位,錯誤修正二
ipp ons AC 信息 解釋 企業號 android bsp ner 在之前的博客中微信企業號開發:微信考勤百度地圖定位,錯誤修正介紹了一種修正百度地圖錯誤的方法
JavaScript--百度地圖定位,可拖拽
event 控件 int pla location view api 事件 ont <html> <head> <meta http-equiv="Content-Type" content="text/html; char
百度地圖定位失敗162問題
http inf bubuko 百度 img 刪掉 。。 .so 程序 今天突然要改程序,發現之前寫的定位的一直返回4.9E-324.百度文檔說是因為.so包的問題,但是看來半天我有.so包啊!為什麽還不行。。。。。沒有編譯 ,一開始我直接復制的文檔裏的這個放上去後還是不行
Android開發-基於百度地圖API開發仿滴滴出行APP介面的實現
前 言 近年來,由於移動網際網路快速的發展以及基於移動裝置的APP的普及,移動網際網路改變了人們的生活方式。從線上的電子支付到線下的出行,移動網際網路是當今社會人們生活不可或缺的一部分,而線下出行的網約車的出現極大便利了人們的出行,雖然它飽受了很大的爭議,但不可否認的是網約車的出現是
百度地圖-定位不準的問題
情況描述:用百度定位做的打卡功能,用資料流量定位,華為小米手機能成功定位當前位置,換個品牌手機,定位位置偏差到旁邊200m外的小區 資料參考 上面這段是百度官方文件裡面的解釋: 這個我 在做的時候基本是這種情況, 文件裡面提到的 精度取決於晶片的問題,其實就是不同手機
基於百度地圖API刪除指定的覆蓋物
引用百度地圖API過程中,想刪除指定的覆蓋物,有很多很多的坑。什麼clearOverlay()方法,直接給我所有覆蓋物都給乾沒了。還有網上介紹的removeOverlay(物件),這種又介紹不清楚,然後自己經過實踐,最終解決了。 直接上圖、上程式碼。 查詢所有地圖的覆蓋物程式碼: /
wex5 如何利用 百度地圖 定位 和 天氣外掛
引包: require("cordova!cordova-plugin-geolocation"); require("cordova!com.justep.cordova.plugin.baidulocation"); Model.prototy
百度地圖定位顯示
//單個地圖顯示 <html> <head> <title>百度地圖</title> <script src="http://api.map.baidu.com/api?v=1.4" type="
Android studio3.0對於百度地圖api開發(2)——百度地圖定位指定地點以及地圖型別的變換
承接上一篇文章寫的,https://blog.csdn.net/qq_41562408/article/details/82794772已經實現了百度地圖的工具準備以及基本的地圖顯示,不過就一個地圖應用來說,只是單純的顯示一種地圖型別以及只是能夠定位到天安門其他地
Android studio3.0對於百度地圖api開發(3)——百度地圖定位當前地點以及地圖覆蓋物簡介
承接上文,上文已經實現了按照緯經度進行地點的定位以及不同地圖型別的切換,https://blog.csdn.net/qq_41562408/article/details/82802082但是就我們普通使用者的需求來說,這遠遠不夠。使用者更希望能夠直接顯示自己的當前位置
Android開發之百度地圖定位以及簡單覆蓋物的實現
直接上程式碼: 先看下效果圖: 我這裡主要做了三個功能: 1.一秒鐘實時定位功能; 2.新增任意經緯度地點到地圖上; 3.判斷朝陽門是否在本人定位範圍1000米內; 百度地圖初始化方法: DemoApplication.java package com
百度地圖定位開發流程
百度地圖是我們日常生活中必不可少的,他能幫助我們找到自己想去的地方,方便出行等等。 下面我就來介紹一下怎樣去使用百度地圖來實現自定位功能,跟著我的思路往下走。。。。。。 選中導航欄的開發文件再點開Android定位SDK,再根據官網的流程講解,一步一步的跟著他
基於百度地圖API的WinForm地圖
大概去年的這個時候,我用VB寫了一個百度地圖的Demo,使用了webBrowser載入本地網頁的方式,只是限於當時的技術,好多功能都沒實現,昨天,我重新對這個程式進行了編寫,這一次我使用的是C#。在正式開始之前,先來解決幾個問題。 一、地圖
安卓開發關於百度地圖定位返回經緯度4.9E324引數 解決方案
百度地圖提供的sdk開發包搞不好就被坑了. Android機型適配不夠好,部分手機能夠定位有些手機不能定位!!!!! 用百度定位的時候一直定位不到,拿到的經緯度都是4.9E-324,Google了一下,發現不少人出現這個問題,經過一番研究,最終解決了這個問題。 目