PhoneGap3.0 使用百度地图SDK定位

天很低 发布于 2013/10/14 02:18
阅读 8K+
收藏 7

按照百度地图SDK官方开发文档引入到项目里,并按照PhoneGap文档添加插件,

js文件,调用方法:

var cb = function(data){ //处理定位结果
               alert(data.Longitude); 
}; 
window.Location(cb,function(msg){
 alert(msg);
}); 
window.Location = function(success,fail,act) {
	if(act){
		var action = "stop";
	}else{
		var action = "get";
	}
	var code = {
		"61": "GPS定位结果",
		"62": "扫描整合定位依据失败。此时定位结果无效。",
		"63": "网络异常,没有成功向服务器发起请求。此时定位结果无效。",
		"65": "定位缓存的结果。",
		"66": "离线定位结果。通过调用时对应的返回结果",
		"67": "离线定位失败。通过调用时对应的返回结果",
		"68": "网络连接失败时,查找本地离线定位时对应的返回结果",
		"161": "表示网络定位结果",
		"162": "服务端定位失败。"
	};
	
    cordova.exec(function(pos){
		var errcode = pos.LocType;
			if(errcode == 61 || errcode == 65 || errcode == 161){
				success(pos);
			}else{
				if(typeof(fail)!="undefined"){
					if(errcode>=162){
						errcode = 162;
					}
					fail(code[errcode])
				};
			}
	},function(err){
		
	},"BaiDuMapLocation", action ,[]);
};

建立插件程序

package com.phonegap.helloworld;

import android.util.Log;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class BaiDuLoc extends CordovaPlugin { 

	private LocationClient mLocationClient = null;
	private JSONObject jsonObj = new JSONObject(); 
	private boolean result = false; 
	private CallbackContext callbackContext;
  	public boolean execute(String action, JSONArray args,final CallbackContext callbackContext) {
	  setCallbackContext(callbackContext);
      if (action.equals("get")) {        	
        	cordova.getActivity().runOnUiThread(new Runnable(){

        		public void run() {
        			mLocationClient = new LocationClient(cordova.getActivity());
        			LocationClientOption option = new LocationClientOption();
        	        option.setOpenGps(false);							
        	        option.setCoorType("bd09ll");							
        	        option.setPriority(LocationClientOption.NetWorkFirst);	
        	        option.setProdName("BaiduLoc");							
        	        option.setScanSpan(5000);								
        	        mLocationClient.setLocOption(option);
       	        
                	mLocationClient.registerLocationListener(new BDLocationListener(){			
            			public void onReceiveLocation(BDLocation location) {
            				if (location == null)
            					return;					
            				try {
            					jsonObj.put("Latitude",location.getLatitude());
            					jsonObj.put("Longitude", location.getLongitude());
            					jsonObj.put("LocType", location.getLocType());
            					jsonObj.put("Radius", location.getRadius());
           					
            					if (location.getLocType() == BDLocation.TypeGpsLocation){
            						jsonObj.put("Speed", location.getSpeed());
            						jsonObj.put("SatelliteNumber", location.getSatelliteNumber());
            					} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
            						jsonObj.put("AddrStr", location.getAddrStr());
            					}
            					Log.d("BaiduMaps", "run success"+jsonObj.toString());
            					callbackContext.success(jsonObj);            					
            					result = true;
            				} catch (JSONException e) {
            					callbackContext.error(e.getMessage());
            					result = true;
            				}
            				
            			}
            			
            	
            			public void onReceivePoi(BDLocation poiLocation) {
            				// TODO Auto-generated method stub		
            			}	
            		}	);
                	mLocationClient.start();
                    mLocationClient.requestLocation();                    
        		}
        		
        	});            
        } else if(action.equals("stop")) {
        	mLocationClient.stop();
        	callbackContext.success(200);
        } else {
        	callbackContext.error(PluginResult.Status.INVALID_ACTION.toString());
        }
      
 		while (result == false) {
 			try {
 				Thread.sleep(100);
 			} catch (InterruptedException e) {
 			}
 		}		 	     
 		return result;
    }	
	
	public void onDestroy(){
    	if (mLocationClient != null && mLocationClient.isStarted()){
    		mLocationClient.stop();
    		mLocationClient = null;
    	}
    	super.onDestroy();
    }
	
	
	public CallbackContext getCallbackContext() {
		return callbackContext;
	}

	public void setCallbackContext(CallbackContext callbackContext) {
		this.callbackContext = callbackContext;
	}
}
加载中
0
axhack
axhack
谢谢分享
0
潺潺
潺潺
用不了。插件一用就卡机定位效果出不了。
0
天很低
天很低

运行效果:

攻心小虫
攻心小虫
地图还是采用js获取吗,不是sdk中接口获取?
0
0
攻心小虫
攻心小虫
谢谢分享,正好可以用起来。
返回顶部
顶部