Hello社区, 我在 Android 中为 cordova 2.9.0 创建了一个插件,以获取位置坐标。 以下是我的代码。
package com.appstudioz.realstate;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
public class GpsLocation extends CordovaPlugin {
public static final String NATIVE_ACTION_STRING="getLocation";
protected LocationManager locationManager;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(NATIVE_ACTION_STRING.equals(action)){
this.showCurrentLocation(callbackContext);
return true;
}
return false;
}
protected void showCurrentLocation(CallbackContext callbackContext) {
String provider=locationManager.getBestProvider(new Criteria(), false);
if(provider!=null){
Location location = locationManager.getLastKnownLocation(provider);
System.out.println("--------------in locationManager");
if (location != null) {
String message = String.format("Current Location",location.getLongitude(), location.getLatitude());
System.out.println("--------------in showCurrenenter code heretLocation---"+message);
callbackContext.success(message);`enter code here`
//callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, message));
}
else{
System.out.println("in showCurrentLocation error");
callbackContext.error("Unable to get location.");
//callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "Unable to get location"));
}
}
}
private LocationManager getSystemService(String locationService) {
// TODO Auto-generated method stub
return null;
}
}
这是javascript文件
var gpsPlugin = {
callNativeFunction: function (success, fail, resultType) {
console.log("---------------------Working");
return cordova.exec( success, fail, "GpsLocation", "getLocation", [resultType]);
}
};
我从这行开始称呼它
gpsPlugin.callNativeFunction(nativePluginResultHandler, nativePluginErrorHandler, 'test');
插件正在运行但是提供
09-21 09:22:19.154:E / Web Console(2023):未捕获错误错误在NPObject上调用方法。在file:///android_asset/www/js/cordova.js:863
如果我没有使用位置服务并从java文件传递字符串消息,则它永远不会出现上述错误。
我浪费了几个小时来调试这个但却无法找到问题。 请帮帮我。
非常感谢!