代码片段(2)
[代码] [Java]代码
001 | package com.yourname.app; |
004 | import android.util.Log; |
006 | import com.baidu.location.BDLocation; |
007 | import com.baidu.location.BDLocationListener; |
008 | import com.baidu.location.LocationClient; |
009 | import com.baidu.location.LocationClientOption; |
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; |
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 ; |
025 | public PluginResult execute(String action, JSONArray args, String callbackId) { |
027 | if (action.equals( "get" )) { |
029 | cordova.getActivity().runOnUiThread( new RunnableLoc()); |
031 | } else if (action.equals( "stop" )) { |
032 | mLocationClient.stop(); |
033 | result = new PluginResult(PluginResult.Status.OK); |
035 | result = new PluginResult(PluginResult.Status.INVALID_ACTION); |
040 | while ( this .result == null ) { |
043 | } catch (InterruptedException e) { |
056 | public void onDestroy(){ |
057 | if (mLocationClient != null && mLocationClient.isStarted()){ |
058 | mLocationClient.stop(); |
059 | mLocationClient = null ; |
065 | class RunnableLoc implements Runnable { |
068 | mLocationClient = new LocationClient(cordova.getActivity()); |
069 | LocationClientOption option = new LocationClientOption(); |
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); |
078 | mLocationClient.registerLocationListener( myListener ); |
079 | mLocationClient.start(); |
080 | mLocationClient.requestLocation(); |
087 | public class MyLocationListenner implements BDLocationListener { |
089 | public void onReceiveLocation(BDLocation location) { |
090 | if (location == null ) |
094 | jsonObj.put( "Latitude" ,location.getLatitude()); |
095 | jsonObj.put( "Longitude" , location.getLongitude()); |
096 | jsonObj.put( "LocType" , location.getLocType()); |
097 | jsonObj.put( "Radius" , location.getRadius()); |
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()); |
106 | result = new PluginResult(PluginResult.Status.OK, jsonObj); |
108 | } catch (JSONException e) { |
110 | result = new PluginResult(PluginResult.Status.JSON_EXCEPTION); |
116 | public void onReceivePoi(BDLocation poiLocation) { |
[代码] [JavaScript]代码
01 | window.Location = function (success,fail,act) { |
07 | cordova.exec( function (pos){ |
08 | var errcode = pos.LocType; |
09 | if (errcode == 61 || errcode == 65 || errcode == 161){ |
14 | },fail, "BaiduLocPlugin" , action , []); |