百度定位
在安卓里面接入百度定位:
1.首先定义变量
public static LocationClient mLocationClient = null;
public static Object objLock = new Object();
public BDLocationListener myBaiDuListener = new MyLocationListener();
static Timer timer = null;
static String roomInfo = "";
2.添加定位回调
public class MyLocationListener implements BDLocationListener {
@Override
public void onConnectHotSpotMessage(String arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onReceiveLocation(BDLocation location) {
//获取定位结果
if (location == null)
{
MethodToLuaJ.getInstance().onBaiDuResp(0+","+0);
synchronized (objLock) {
if(mLocationClient != null && mLocationClient.isStarted()){
mLocationClient.stop();
}
}
return;
}
Log.w("baidu", Integer.toString(location.getLocType()));
double latitude = location.getLatitude();
double longitude = location.getLongitude();
if (location.getLocType() == BDLocation.TypeGpsLocation){
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {
} else{
latitude = 0;
longitude = 0;
}
Log.i("baidu", latitude+","+longitude);
//Toast.makeText(instance, latitude+"=="+longitude, Toast.LENGTH_LONG).show();
MethodToLuaJ.getInstance().onBaiDuResp(latitude+","+longitude);
synchronized (objLock) {
if(mLocationClient != null && mLocationClient.isStarted()){
mLocationClient.stop();
}
}
}
}
3. 初始化
private void initLocation(){
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);
option.setCoorType("bd09ll");
//可选,默认gcj02,设置返回的定位结果坐标系
//int span=0;
//option.setScanSpan(0);
//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setIgnoreKillProcess(false);
option.setIsNeedAddress(true);
//可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);
//可选,默认false,设置是否使用gps
synchronized (objLock) {
if(mLocationClient == null){
mLocationClient = new LocationClient(getApplicationContext());
}
mLocationClient.setLocOption(option);
mLocationClient.registerLocationListener(myBaiDuListener);
}
if(option != null){
synchronized (objLock) {
if(mLocationClient == null){
mLocationClient = new LocationClient(getApplicationContext());
}
if(mLocationClient.isStarted()){
mLocationClient.stop();
}
mLocationClient.setLocOption(option);
mLocationClient.registerLocationListener(myBaiDuListener);
}
}
}
4. 添加开启定位方法
public static void StartBaiDuLocation()
{
Log.w("baidu","StartBaiDuLocation11");
synchronized (objLock) {
if(mLocationClient != null && !mLocationClient.isStarted()){
mLocationClient.start();//开启定位
}
}
}
到了这里java代码就算是完成了,还要加一个从java到lua的桥梁,这里暂时是onBaiDuResp
public void onBaiDuResp(final String arg){
appActivity.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxLuaJavaBridge.callLuaGlobalFunctionWithString("onBaiDuResp", arg);
}
});
}
5. 开启定位
--开启百度定位(有的时候获取是0,所以可能需要多次定位)
function onStartBaiDuLocation()
print("onStartBaiDuLocation -----")
if device.platform == "ios" then
local ok,ret = luaCallFun.callStaticMethod("AppController","StartBaiDuLocation")
if ok == false then
prints("luaoc调用出错:StartBaiDuLocation")
end
elseif device.platform == "android" then
print("start lua to java");
local javaMethodName = "StartBaiDuLocation"
local javaParams = {}
local ok,ret = luaCallFun.callStaticMethod(javaClassName, javaMethodName, javaParams)
if ok == false then
prints("luaoc调用出错 onStartBaiDuLocation")
end
else
prints("onStartBaiDuLocation!")
end
End
这是从lua到java的桥梁
在适当的地方调用,然后在安卓libs文件夹下面添加百度定位的jar包.
最后附上一个计算距离的方法:
function DistanceLayer:LantitudeLongitudeDist(lat1, lon1, lat2, lon2)
local EARTH_RADIUS = 6378.137;
-- 赤道半径(单位m)
local function rad(lonlat)
return lonlat * math.pi / 180.0;
end
local radLat1 = rad(lat1);
local radLat2 = rad(lat2);
local a = radLat1 - radLat2;
local b = rad(lon1) - rad(lon2);
local s = 2 * math.asin( math.sqrt( math.pow(math.sin(a / 2), 2) + math.cos(radLat1) * math.cos(radLat2) * math.pow(math.sin(b / 2), 2)));
s = s * EARTH_RADIUS;
s = math.round(s * 100) / 100;
s = s * 1000;
return s;
end