PhoneGap的百度定位API插件
PhoneGap的地理定位API实际上使用的是HTML5的方法,在我开发过程中发现不少手机并不能使用HTML5的定位方法,估计是ROM默认拒绝了请求。本插件使用百度定位API的SDK开发,目前还比较简单只是简单的使用NATIVE调用百度API获取经纬坐标返回给前端JS。
github: https://github.com/phpcxy/PhoneGap-BaiduLocPlugin
github: https://github.com/phpcxy/PhoneGap-BaiduLocPlugin
标签: PhoneGap
代码片段(2)
[代码] [Java]代码
001 |
package com.yourname.app; |
002 |
003 |
004 |
import android.util.Log; |
005 |
006 |
import com.baidu.location.BDLocation; |
007 |
import com.baidu.location.BDLocationListener; |
008 |
import com.baidu.location.LocationClient; |
009 |
import com.baidu.location.LocationClientOption; |
010 |
011 |
import org.apache.cordova.api.Plugin; |
012 |
import org.apache.cordova.api.PluginResult; |
013 |
import org.json.JSONArray; |
014 |
import org.json.JSONException; |
015 |
import org.json.JSONObject; |
016 |
017 |
018 |
019 |
public class BaiduLocPlugin extends Plugin { |
020 |
private LocationClient mLocationClient = null ; |
021 |
private MyLocationListenner myListener = new MyLocationListenner(); |
022 |
private JSONObject jsonObj = new JSONObject(); |
023 |
private PluginResult result = null ; |
024 |
|
025 |
public PluginResult execute(String action, JSONArray args, String callbackId) { |
026 |
|
027 |
if (action.equals( "get" )) { |
028 |
|
029 |
cordova.getActivity().runOnUiThread( new RunnableLoc()); |
030 |
|
031 |
} else if (action.equals( "stop" )) { |
032 |
mLocationClient.stop(); |
033 |
result = new PluginResult(PluginResult.Status.OK); |
034 |
} else { |
035 |
result = new PluginResult(PluginResult.Status.INVALID_ACTION); |
036 |
} |
037 |
|
038 |
|
039 |
// waiting ui thread to finish |
040 |
while ( this .result == null ) { |
041 |
try { |
042 |
Thread.sleep( 100 ); |
043 |
} catch (InterruptedException e) { |
044 |
// ignoring exception, since we have to wait |
045 |
// ui thread to finish |
046 |
} |
047 |
} |
048 |
|
049 |
|
050 |
return this .result; |
051 |
|
052 |
|
053 |
} |
054 |
|
055 |
@Override |
056 |
public void onDestroy(){ |
057 |
if (mLocationClient != null && mLocationClient.isStarted()){ |
058 |
mLocationClient.stop(); |
059 |
mLocationClient = null ; |
060 |
} |
061 |
super .onDestroy(); |
062 |
} |
063 |
|
064 |
|
065 |
class RunnableLoc implements Runnable { |
066 |
|
067 |
public void run() { |
068 |
mLocationClient = new LocationClient(cordova.getActivity()); |
069 |
LocationClientOption option = new LocationClientOption(); |
070 |
|
071 |
option.setOpenGps( false ); |
072 |
option.setCoorType( "bd09ll" ); |
073 |
option.setPriority(LocationClientOption.NetWorkFirst); |
074 |
option.setProdName( "BaiduLoc" ); |
075 |
option.setScanSpan( 5000 ); |
076 |
mLocationClient.setLocOption(option); |
077 |
|
078 |
mLocationClient.registerLocationListener( myListener ); |
079 |
mLocationClient.start(); |
080 |
mLocationClient.requestLocation(); |
081 |
|
082 |
} |
083 |
084 |
} |
085 |
|
086 |
|
087 |
public class MyLocationListenner implements BDLocationListener { |
088 |
|
089 |
public void onReceiveLocation(BDLocation location) { |
090 |
if (location == null ) |
091 |
return ; |
092 |
|
093 |
try { |
094 |
jsonObj.put( "Latitude" ,location.getLatitude()); |
095 |
jsonObj.put( "Longitude" , location.getLongitude()); |
096 |
jsonObj.put( "LocType" , location.getLocType()); |
097 |
jsonObj.put( "Radius" , location.getRadius()); |
098 |
|
099 |
if (location.getLocType() == BDLocation.TypeGpsLocation){ |
100 |
jsonObj.put( "Speed" , location.getSpeed()); |
101 |
jsonObj.put( "SatelliteNumber" , location.getSatelliteNumber()); |
102 |
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){ |
103 |
jsonObj.put( "AddrStr" , location.getAddrStr()); |
104 |
} |
105 |
|
106 |
result = new PluginResult(PluginResult.Status.OK, jsonObj); |
107 |
|
108 |
} catch (JSONException e) { |
109 |
// TODO Auto-generated catch block |
110 |
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION); |
111 |
} |
112 |
|
113 |
} |
114 |
|
115 |
|
116 |
public void onReceivePoi(BDLocation poiLocation) { |
117 |
// TODO Auto-generated method stub |
118 |
|
119 |
} |
120 |
|
121 |
|
122 |
} |
123 |
|
124 |
125 |
} |
[代码] [JavaScript]代码
01 |
window.Location = function (success,fail,act) { |
02 |
if (act){ |
03 |
var action = act; |
04 |
} else { |
05 |
var action = 'get' ; |
06 |
} |
07 |
cordova.exec( function (pos){ |
08 |
var errcode = pos.LocType; |
09 |
if (errcode == 61 || errcode == 65 || errcode == 161){ |
10 |
success(pos); |
11 |
} else { |
12 |
fail(errcode); |
13 |
} |
14 |
},fail, "BaiduLocPlugin" , action , []); |
15 |
}; |