cocos2dx接百度定位SDK(Android)

步骤:

第一步:

到百度地图开发平台申请AK,SHA1获得方式如图

第二步:

下载SDK:BaiduLBS_AndroidSDK_Lib,把文件夹内容拷贝到工程/proj.android/libs目录下

第三步:

在AndroidManifest.xml的application下添加

<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="自己项目的AK" >
</meta-data>
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
<intent-filter>
<action android:name="com.baidu.location.service_v2.2" >
</action>
</intent-filter>
</service>
<!-- 这个权限用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!-- 这个权限用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<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" />
<!-- SD卡读取权限,用户写入离线定位数据-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

第四步:

在AppActivity.java中添加

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.location.Poi;
public class AppActivity extends Cocos2dxActivity {
// 百度定位
	public static LocationClient mLocationClient = null;
	private static MyLocationListener myListener = null;
	public static native void Result(String res);

	//----------百度定位功能开始--------
	// 初始化
	public static void InitLocation()
    {
		if(mLocationClient == null)
		{
			mLocationClient = new LocationClient(AppActivity.getInstance());   
			myListener = new MyLocationListener();		
			mLocationClient.registerLocationListener(myListener); 
		}
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);
		option.setCoorType("bd09ll");
		option.setScanSpan(1000);
		option.setOpenGps(true);
		option.setLocationNotify(true);
		option.setIgnoreKillProcess(false);
		option.SetIgnoreCacheException(false);
		option.setWifiCacheTimeOut(5*60*1000);
		option.setEnableSimulateGps(false);
		mLocationClient.setLocOption(option);
	}
	
	// 开始定位
	public static void StartLocate()
	{
		mLocationClient.start();
	}
    
	// 回调结果
	public static class MyLocationListener implements BDLocationListener 
	{
		@Override
		public void onReceiveLocation(BDLocation location)
		{
			double latitude = location.getLatitude();    //获取纬度信息
			double longitude = location.getLongitude();  //获取经度信息
			float radius = location.getRadius();    	 //获取定位精度,默认值为0.0f		
			String coorType = location.getCoorType();	 //获取经纬度坐标类型		
			int errorCode = location.getLocType();		 //获取定位类型、定位错误返回码		
			Result(latitude, longitude);
		}
	}
    //----------百度定位功能结束--------
}

第五步:

在Android.mk中添加

#####################################################
include $(CLEAR_VARS)
LOCAL_MODULE := liblocSDK7b
LOCAL_SRC_FILES := ../../Classes/Core/Utils/PhoneUtils/android/BaiduLBS/libs/Android/$(TARGET_ARCH_ABI)/liblocSDK7b.so
include $(PREBUILT_SHARED_LIBRARY)
#####################################################

# _COCOS_LIB_ANDROID_BEGIN
#####################################################
LOCAL_SHARED_LIBRARIES += liblocSDK7b
#####################################################
# _COCOS_LIB_ANDROID_END

第六步:

C++代码

#include "LocationManager.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#endif

void LocationManager::OnResult(double latitude, double longitude)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    if (OnLocationResult) OnLocationResult(latitude, longitude);
    OnLocationResult = nullptr;
#endif
}

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
extern "C" {
    void Java_org_cocos2dx_cpp_AppActivity_Result(JNIEnv *env, jobject obj, jstring jresult)
    {
        std::string result = env->GetStringUTFChars(jresult, nullptr);
        LocationManager::getInstance()->OnResult(result);
    }
}
#endif

bool Locate()
{
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    cocos2d::JniMethodInfo payM;
    bool isHave = cocos2d::JniHelper::getStaticMethodInfo(payM, "org/cocos2dx/cpp/AppActivity", "StartLocate",	"()V");
    bool resulte = false;
    if (isHave)
    {
        payM.env->CallStaticVoidMethod(payM.classID, payM.methodID);
    }
    else
    {
        return  false;
        CCLOG("get StartLocate function is fail");
    }
    
#endif
    return true;
    
}

void LocationManager::InitLocation()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    
    cocos2d::JniMethodInfo payM;
    bool isHave = cocos2d::JniHelper::getStaticMethodInfo(payM, "org/cocos2dx/cpp/AppActivity", "InitLocation","()V");
    if (isHave)
    {
        payM.env->CallStaticVoidMethod(payM.classID, payM.methodID);
    }
    else
    {
        CCLOG("get InitLocation function is fail");
    }
#endif
}

bool LocationManager::GetCity(std::function<void(std::string)> _OnLocationResult)
{
    InitLocation();
    OnLocationResult = _OnLocationResult;
    return Locate();
}

第七步:

OJBK!




阅读更多
文章标签: SDK
个人分类: cocos2d
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭