美文网首页程序猿Android unity的交互
Android和Unity3D之高德地图(三)

Android和Unity3D之高德地图(三)

作者: 茶杯里的阳光 | 来源:发表于2018-07-12 18:28 被阅读20次

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


    image.png

    下载并且导入依赖


    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中,修改如下
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xc.mylibrary"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <!--用于读取手机当前的状态-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <!--用于写入缓存数据到扩展存储卡-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    
    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true">
        <service android:name="com.amap.api.location.APSService"></service>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data android:name="com.amap.api.v2.apikey" android:value="************************">//你项目对应的key值
        </meta-data>
    </application>
    

    </manifest>

    同理 生成jar


    image.png

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


    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<AndroidJavaObject>("currentActivity");
        text.text = jo.Call<string>("GetInfo");
    }
    

    }

    完成对接 获取到信息

    相关文章

      网友评论

        本文标题:Android和Unity3D之高德地图(三)

        本文链接:https://www.haomeiwen.com/subject/enirpftx.html