分享

PhoneGap的百度定位API插件

 看见就非常 2013-03-21


PhoneGap的百度定位API插件

phpcxy 发布于 2012年07月28日 16时, 0评/1036阅
PhoneGap的地理定位API实际上使用的是HTML5的方法,在我开发过程中发现不少手机并不能使用HTML5的定位方法,估计是ROM默认拒绝了请求。本插件使用百度定位API的SDK开发,目前还比较简单只是简单的使用NATIVE调用百度API获取经纬坐标返回给前端JS。 

github:  https://github.com/phpcxy/PhoneGap-BaiduLocPlugin  

标签: PhoneGap

代码片段(2)

[代码] [Java]代码

001package com.yourname.app;
002 
003 
004import android.util.Log;
005 
006import com.baidu.location.BDLocation;
007import com.baidu.location.BDLocationListener;
008import com.baidu.location.LocationClient;
009import com.baidu.location.LocationClientOption;
010 
011import org.apache.cordova.api.Plugin;
012import org.apache.cordova.api.PluginResult;
013import org.json.JSONArray;
014import org.json.JSONException;
015import org.json.JSONObject;
016 
017 
018 
019public 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]代码

01window.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};

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多