有关Cocos项目里面接入高德SDK的方式
咳咳,写博客了! 1.有关高德开放平台的审核工作
高德开放平台传送门
首先就是要注册账号,分为个人开发者和企业开发者两种身份,企业开发者审核期为三天,权限和服务也会更多。然后创建自己的应用,填写信息,如下:
有关SHA1等信息可以在项目里面查看:
点击Preferences
这里有两个keystore,一个是debug版本的,另一个是release版本的,有关keystore文件的生成请点击:传送门
然后把release的keystore信息填写到高德上就可以得到一个key值了。接下来就是要写代码了
|
1
|
<service android:name=
"com.amap.api.location.APSService"
></service>
|
然后,声明权限
Android 6.0及以上系统可以参考Android 6.0权限说明章节。
<uses-permission android:name=
"android.permission.ACCESS_COARSE_LOCATION"
></uses-permission>
<!--用于访问GPS定位-->
"android.permission.ACCESS_FINE_LOCATION"
></uses-permission>
<!--用于获取运营商信息,用于支持提供运营商信息相关的接口-->
"android.permission.ACCESS_NETWORK_STATE"
></uses-permission>
<!--用于访问wifi网络信息,wifi信息会用于进行网络定位-->
"android.permission.ACCESS_WIFI_STATE"
></uses-permission>
<!--用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
"android.permission.CHANGE_WIFI_STATE"
></uses-permission>
<!--用于访问网络,网络定位需要上网-->
"android.permission.INTERNET"
></uses-permission>
<!--用于读取手机当前的状态-->
"android.permission.READ_PHONE_STATE"
></uses-permission>
<!--用于写入缓存数据到扩展存储卡-->
"android.permission.WRITE_EXTERNAL_STORAGE"
></uses-permission>
<!--用于申请调用A-GPS模块-->
"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"
></uses-permission>
<!--用于申请获取蓝牙信息进行室内定位-->
"android.permission.BLUETOOTH"
></uses-permission>
"android.permission.BLUETOOTH_ADMIN"
></uses-permission>
接下来需要在org.cocos2dx.lua.AppActivity里面添加初始代码(由于这是一个获取位置的小Demo,目前只有满足需求的部分代码,后续增加功能的话,其余功能代码会陆续加上来)
/**************************************************************************** Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2011 Zynga Inc. Copyright (c) 2013-2014 Chukong Technologies Inc. http://www.cocos2d-x.org Permission is hereby granted,free of charge,to any person obtaining a copy of this software and associated documentation files (the "Software"),to deal in the Software without restriction,including without limitation the rights to use,copy,modify,merge,publish,distribute,sublicense,and/or sell copies of the Software,and to permit persons to whom the Software is furnished to do so,subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ package org.cocos2dx.lua; import org.cocos2dx.lib.Cocos2dxActivity; import android.os.Bundle; import android.util.Log; import android.content.Intent; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationClientOption.AMapLocationMode; import com.amap.api.location.AMapLocationClientOption.AMapLocationProtocol; import com.amap.api.location.AMapLocationListener; import com.anysdk.framework.PluginWrapper; public class AppActivity extends Cocos2dxActivity { private AMapLocationClient locationClient = null; private AMapLocationClientOption locationOption = new AMapLocationClientOption(); private String LocationInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initLocation(); //for anysdk PluginWrapper.init(this); } public String GetInfo() { return LocationInfo; } /** * 初始化定位 */ private void initLocation(){ //初始化client locationClient = new AMapLocationClient(this.getApplicationContext()); //设置定位参数 locationClient.setLocationOption(getDefaultOption()); // 设置定位监听 locationClient.setLocationListener(locationListener); //启动定位 locationClient.startLocation(); } AMapLocationListener locationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation location) { if (location != null) { if (location.getErrorCode() == 0) { StringBuffer sb = new StringBuffer(256); sb.append("时间: "); sb.append(location.getTime()); sb.append("n纬度:"); sb.append(location.getLatitude()); sb.append("n纬度:"); sb.append(location.getLongitude()); sb.append("n精度:"); sb.append(location.getAccuracy()); sb.append("n地址:"); sb.append(location.getAddress()); sb.append("n国家信息:"); sb.append(location.getCountry()); sb.append("n省信息:"); sb.append(location.getProvince()); sb.append("n城市信息:"); sb.append(location.getCity()); sb.append("n城区信息:"); sb.append(location.getDistrict()); sb.append("n街道信息:"); sb.append(location.getStreet()); sb.append("n街道门牌号信息:"); sb.append(location.getStreetNum()); sb.append("n城市编码:"); sb.append(location.getCityCode()); sb.append("n地区编码:"); sb.append(location.getAdCode()); sb.append("n定位点AOI信息:"); sb.append(location.getAoiName()); LocationInfo = sb.toString(); Log.d("LOC",LocationInfo); }else { //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 Log.e("AmapError","location Error,ErrCode:" + location.getErrorCode() + ",errInfo:" + location.getErrorInfo()); } } } }; /** * 默认的定位参数 */ private AMapLocationClientOption getDefaultOption(){ AMapLocationClientOption mOption = new AMapLocationClientOption(); mOption.setLocationMode(AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式 mOption.setGpsFirst(false);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭 mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效 mOption.setInterval(2000);//可选,设置定位间隔。默认为2秒 mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true mOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用 AMapLocationClientOption.setLocationProtocol(AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP mOption.setSensorEnable(false);//可选,设置是否使用传感器。默认是false mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差 mOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为true return mOption; } @Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ super.onActivityResult(requestCode,resultCode,data); PluginWrapper.onActivityResult(requestCode,data); } @Override protected void onResume() { super.onResume(); PluginWrapper.onResume(); } @Override public void onPause(){ PluginWrapper.onPause(); super.onPause(); } @Override protected void onNewIntent(Intent intent) { PluginWrapper.onNewIntent(intent); super.onNewIntent(intent); } }
private AMapLocationClient locationClient = null;//声明AMapLocationClient类对象 private AMapLocationClientOption locationOption = new AMapLocationClientOption();//声明并初始化AMapLocationClientOption对象 private String LocationInfo;//保存获取到的地址信息
选择定位模式
高德定位服务包含GPS和网络定位(Wi-Fi和基站定位)两种能力。定位SDK将GPS、网络定位能力进行了封装,以三种定位模式对外开放,SDK默认选择使用高精度定位模式。
高精度定位模式:会同时使用网络定位和GPS定位,优先返回最高精度的定位结果,以及对应的地址描述信息。
//设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);