Android和Unity3D之高德地图(三)

首先去高德地图官网下载定位功能的jar文件


Android和Unity3D之高德地图(三)_第1张图片
image.png

下载并且导入依赖


Android和Unity3D之高德地图(三)_第2张图片
image.png

在MaiActivity中代码如下 获取定位信息

//声明AMapLocationClient类对象
public AMapLocationClient mLocationClient = null;
//声明AMapLocationClientOption对象
public AMapLocationClientOption mLocationOption = null;
//定位回调字符串
private String LocationInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

public String GetInfo(){
    startLocation();
    return LocationInfo;
}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
}

private void startLocation(){
    //初始化定位
    mLocationClient = new AMapLocationClient(getApplicationContext());
    //设置定位回调监听
    mLocationClient.setLocationListener(mLocationListener);
    //初始化AMapLocationClientOption对象
    mLocationOption = new AMapLocationClientOption();
    //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。
    mLocationOption.setInterval(2000);
    //给定位客户端对象设置定位参数
    mLocationClient.setLocationOption(mLocationOption);
    //启动定位
    mLocationClient.startLocation();
}

public AMapLocationListener mLocationListener = new AMapLocationListener() {

    @Override
    public void onLocationChanged(AMapLocation location) {
        // TODO Auto-generated method stub
        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();
            }else {
                //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
                Log.e("AmapError","location Error, ErrCode:"
                        + location.getErrorCode() + ", errInfo:"
                        + location.getErrorInfo());
            }
        }
    }
};

在AndroidMainfest.xml中,修改如下

package="com.xc.mylibrary"
android:versionCode="1"
android:versionName="1.0">
android:minSdkVersion="16"
android:targetSdkVersion="21" />













    
    
        
            

            
        
    
    //你项目对应的key值
    

同理 生成jar


Android和Unity3D之高德地图(三)_第3张图片
image.png

然后将指定的两个jar包,导入unity中


Android和Unity3D之高德地图(三)_第4张图片
image.png

并且将AndroidMainfest.xml复制到unity

在unity中

usingusing UnityEngine;
UnityEngine; usingusing System.Collections;
System.Collections using UnityEngine.UI;

public class GetLocationInfo : MonoBehaviour {

public Text text;
// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
}

public void StartLocation() {
    AndroidJavaClass jc = new   AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject jo = jc.GetStatic("currentActivity");
    text.text = jo.Call("GetInfo");
}

}

完成对接 获取到信息

你可能感兴趣的:(Android和Unity3D之高德地图(三))