写了取得经纬度的方法
cocos creator代码如下
var longitude = jsb.reflection.callStaticMethod(“org/cocos2dx/javascript/Test”, “getLongitude”, “()F”);
this.Longitude.string = longitude.toString(); //经度
var latitude = jsb.reflection.callStaticMethod(“org/cocos2dx/javascript/Test”, “getLatitude”, “()F”);
this.Latitude.string = latitude.toString(); //纬度
原生代码如下
package org.cocos2dx.javascript;
import android.content.Context;
import android.location.LocationManager;
import java.util.List;
import android.location.Location;
import java.util.List;
public class Test {
public static float getLongitude() {
LocationManager lm = (LocationManager) AppActivity.getAppContext().getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* 迴圈读取providers,如果有位址资讯, 退出迴圈*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude(); //纬度
gps[1] = l.getLongitude();//经度
}
return (float)gps[1];
}
public static float getLatitude() {
LocationManager lm = (LocationManager) AppActivity.getAppContext().getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* 迴圈读取providers,如果有位址资讯, 退出迴圈*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude(); //纬度
gps[1] = l.getLongitude();//经度
}
return (float)gps[0];
}
}
无回传值,android黑屏
错误讯息 [ERROR] (D:/Project/javaGPS/build/jsb-default/frameworks/cocos2d-x/cocos/scripting/js-bindings/jswrapper/v8/Object.cpp, 534): Invoking function (0xb8a24978) failed!