虽然不难。。能简单搞定的就简单搞。。能复制就复制。
插件地址:https://github.com/DoubleSpout/phonegap_baidu_sdk_location
原文博客:http://snoopyxdy.blog.163.com/blog/static/601174402014420872345/
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" id="com.spout.phonegap.plugins.baidulocation" version="0.1.0">
<name>BaiduLocation</name>
<description>Baidu Location Plugin for Phonegap</description>
<license>MIT</license>
<keywords>baidu, location, phonegap</keywords>
<!-- android -->
<platform name="android">
<js-module src="www/baidulocation.js" name="BiaduLocation">
<clobbers target="window.baiduLocation" />
</js-module>
<config-file target="res/xml/config.xml" parent="/*">
<feature name="BaiduLocation">
<param name="android-package" value="com.spout.phonegap.plugins.baidulocation.BaiduLocation"/>
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"></service>
</config-file>
<source-file src="src/android/BaiduLocation.java" target-dir="src/com/spout/phonegap/plugins/baidulocation" />
<source-file src="src/android/locSDK_4.0.jar" target-dir="libs" framework="true"/>
<source-file src="src/android/liblocSDK4.so" target-dir="libs/armeabi" framework="true"/>
</platform>
</plugin>
将上述地址配置到指定文件即可.
需要留意版本,本人就还是在用android 4.1 cordova 用的是2.8版本,所以稍微麻烦了点
莫鄙视…
补充一个2.8版本的百度地图定位
package com.spout.phonegap.plugins.baidulocation;
import java.util.HashMap;
import java.util.Map;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
public class BaiduLocation extends Plugin {
public static final String PLUGIN_NAME = "BaiduLocation";
NotificationManager notificationManager;
Notification note;
PendingIntent contentIntent;
public CallbackContext callbackContext;
private static final String STOP_ACTION = "stop";
private static final String GET_ACTION = "getCurrentPosition";
public LocationClient locationClient = null;
public JSONObject jsonObj = new JSONObject();
public boolean result = false;
public BDLocationListener myListener;
private static final Map<Integer, String> ERROR_MESSAGE_MAP = new HashMap<Integer, String>();
private static final String DEFAULT_ERROR_MESSAGE = "服务端定位失败";
static {
ERROR_MESSAGE_MAP.put(61, "GPS定位结果");
ERROR_MESSAGE_MAP.put(62, "扫描整合定位依据失败。此时定位结果无效");
ERROR_MESSAGE_MAP.put(63, "网络异常,没有成功向服务器发起请求。此时定位结果无效");
ERROR_MESSAGE_MAP.put(65, "定位缓存的结果");
ERROR_MESSAGE_MAP.put(66, "离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果");
ERROR_MESSAGE_MAP.put(67, "离线定位失败。通过requestOfflineLocaiton调用时对应的返回结果");
ERROR_MESSAGE_MAP.put(68, "网络连接失败时,查找本地离线定位时对应的返回结果。");
ERROR_MESSAGE_MAP.put(161, "表示网络定位结果");
};
public String getErrorMessage(int locationType) {
String result = ERROR_MESSAGE_MAP.get(locationType);
if (result == null) {
result = DEFAULT_ERROR_MESSAGE;
}
return result;
}
private void logMsg(String s) {
System.out.println(s);
}
public CallbackContext getCallbackContext() {
return callbackContext;
}
public void setCallbackContext(CallbackContext callbackContext) {
this.callbackContext = callbackContext;
}
@Override
public boolean execute(String action, JSONArray args,
final CallbackContext callbackContext) throws JSONException {
Log.d(PLUGIN_NAME, "Plugin execute called with action: " + action);
setCallbackContext(callbackContext);
if (GET_ACTION.equals(action)) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(PLUGIN_NAME, "run: ");
locationClient = new LocationClient(cordova.getActivity());
locationClient.setAK("BfkPvjDGHC0ATZhIr6wxnHh9");// 设置百度的ak
myListener = new MyLocationListener();
locationClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
option.setProdName("BaiduLoc");
option.disableCache(true);// 禁止启用缓存定位
locationClient.setLocOption(option);
locationClient.start();
locationClient.requestLocation();
}
});
} else if (STOP_ACTION.equals(action)) {
locationClient.stop();
}
while (result == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return true;
}
@Override
public void onDestroy() {
if (locationClient != null && locationClient.isStarted()) {
locationClient.stop();
locationClient = null;
}
super.onDestroy();
}
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return;
try {
Log.d(PLUGIN_NAME, "MyLocationListener");
JSONObject coords = new JSONObject();
coords.put("latitude", location.getLatitude());
coords.put("longitude", location.getLongitude());
coords.put("radius", location.getRadius());
jsonObj.put("coords", coords);
int locationType = location.getLocType();
jsonObj.put("locationType", locationType);
jsonObj.put("code", locationType);
jsonObj.put("message", getErrorMessage(locationType));
switch (location.getLocType()) {
case BDLocation.TypeGpsLocation:
coords.put("speed", location.getSpeed());
coords.put("altitude", location.getAltitude());
jsonObj.put("SatelliteNumber",
location.getSatelliteNumber());
break;
case BDLocation.TypeNetWorkLocation:
jsonObj.put("addr", location.getAddrStr());
break;
}
Log.d("BaiduLocationPlugin", "run: " + jsonObj.toString());
callbackContext.success(jsonObj);
result = true;
} catch (JSONException e) {
callbackContext.error(e.getMessage());
result = true;
}
}
@Override
public void onReceivePoi(BDLocation arg0) {
// TODO Auto-generated method stub
}
}
@SuppressWarnings("unused")
@Override
public PluginResult execute(String arg0, JSONArray arg1, String arg2) {
Log.d("BaiduLocationPlugin", "run: xxxx" );
return new PluginResult(true ? PluginResult.Status.OK
: PluginResult.Status.ERROR);
// TODO Auto-generated method stub
}
}