前端之家收集整理的这篇文章主要介绍了
有关Cocos项目里面接入高德SDK的方式,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
咳咳,写博客了!
1.有关高德开放平台的审核工作
首先就是要注册账号,分为个人开发者和企业开发者两种身份,企业开发者审核期为三天,权限和服务也会更多。然后创建自己的应用,填写信息,如下:
有关SHA1等信息可以在项目里面查看:
点击Preferences
这里有两个keystore,一个是debug版本的,另一个是release版本的,有关keystore文件的生成请点击:传送门
然后把release的keystore信息填写到高德上就可以得到一个key值了。接下来就是要写代码了
2.项目配置
从官网上面下载jar包等开发工具,然后导入到项目的libs文件夹下面(eclipse),然后右击,如图所示:
接下来需要配置AndroidManifest.xml文件。
为了保证高德 Android SDK 的功能正常使用,您需要申请高德 Key 并且配置到项目中。
项目的 “AndroidManifest.xml” 文件中,添加如下代码:
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<Meta-data
android:name="com.amap.api.v2.apikey"
android:value="请输入您的用户Key"/>
……
</application>
首先,声明Service组件
请在application标签中声明service组件,每个app拥有自己单独的定位service。
1
|
<service android:name=
"com.amap.api.location.APSService"
></service>
|
然后,声明权限
Android 6.0及以上系统可以参考Android 6.0权限说明章节。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!--用于进行网络定位-->
<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>
"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"
></uses-permission>
"android.permission.BLUETOOTH"
></uses-permission>
"android.permission.BLUETOOTH_ADMIN"
></uses-permission>
|
/****************************************************************************
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);
}
}