开通VIP,畅享免费电子书等14项超值服
首页
好书
留言交流
下载APP
联系客服
2013.03.21
001
package
com.yourname.app;
002
003
004
import
android.util.Log;
005
006
com.baidu.location.BDLocation;
007
com.baidu.location.BDLocationListener;
008
com.baidu.location.LocationClient;
009
com.baidu.location.LocationClientOption;
010
011
org.apache.cordova.api.Plugin;
012
org.apache.cordova.api.PluginResult;
013
org.json.JSONArray;
014
org.json.JSONException;
015
org.json.JSONObject;
016
017
018
019
public
class
BaiduLocPlugin
extends
Plugin {
020
private
LocationClient mLocationClient =
null
;
021
MyLocationListenner myListener =
new
MyLocationListenner();
022
JSONObject jsonObj =
JSONObject();
023
PluginResult result =
024
025
PluginResult execute(String action, JSONArray args, String callbackId) {
026
027
if
(action.equals(
"get"
)) {
028
029
cordova.getActivity().runOnUiThread(
RunnableLoc());
030
031
}
else
"stop"
032
mLocationClient.stop();
033
result =
PluginResult(PluginResult.Status.OK);
034
{
035
PluginResult(PluginResult.Status.INVALID_ACTION);
036
037
038
039
// waiting ui thread to finish
040
while
(
this
.result ==
) {
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
.result;
051
052
053
054
055
@Override
056
void
onDestroy(){
057
(mLocationClient !=
&& mLocationClient.isStarted()){
058
059
mLocationClient =
060
061
super
.onDestroy();
062
063
064
065
RunnableLoc
implements
Runnable {
066
067
run() {
068
LocationClient(cordova.getActivity());
069
LocationClientOption option =
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
MyLocationListenner
BDLocationListener {
088
089
onReceiveLocation(BDLocation location) {
090
(location ==
)
091
092
093
094
jsonObj.put(
"Latitude"
,location.getLatitude());
095
"Longitude"
, location.getLongitude());
096
"LocType"
, location.getLocType());
097
"Radius"
, location.getRadius());
098
099
(location.getLocType() == BDLocation.TypeGpsLocation){
"Speed"
, location.getSpeed());
101
"SatelliteNumber"
, location.getSatelliteNumber());
102
(location.getLocType() == BDLocation.TypeNetWorkLocation){
103
"AddrStr"
, location.getAddrStr());
104
105
106
PluginResult(PluginResult.Status.OK, jsonObj);
107
108
(JSONException e) {
109
// TODO Auto-generated catch block
110
PluginResult(PluginResult.Status.JSON_EXCEPTION);
111
112
113
114
115
116
onReceivePoi(BDLocation poiLocation) {
117
// TODO Auto-generated method stub
118
119
120
121
122
123
124
125
01
window.Location =
function
(success,fail,act) {
02
(act){
03
var
action = act;
04
05
action =
'get'
06
07
cordova.exec(
(pos){
08
errcode = pos.LocType;
09
(errcode == 61 || errcode == 65 || errcode == 161){
10
success(pos);
11
12
fail(errcode);
13
14
},fail,
"BaiduLocPlugin"
, action , []);
15
};
微信登录中...请勿关闭此页面