zoukankan      html  css  js  c++  java
  • Unity高德LBS

    一、准备

    高德开发者页面注册产品 

    1.1 http://www.cnblogs.com/U-tansuo/archive/2013/04/25/unity3d_android_sign.html

    1.2填写应用应用信息

    1.3下载Android 定位SDK 

    http://lbs.amap.com/api/android-location-sdk/locationsummary

    解压得到 AMap_Location_V3.6.1_20171012.jar

    二.新建Eclipse项目

    2.1首先新建一个Android工程,在最小SDK版本里选4.0以上,不然在Unity打包的时候会报错。

    2.2导入AMap_Location_V3.6.1_20171012.jarUnity提供的JARclasses.jar

    位置:

    D:Program FilesUnity17103EditorDataPlaybackEnginesAndroidPlayerVariationsil2cppDevelopmentClasses

    2.3 Java代码

    package com.mirror.locationtest;
    
    import android.os.Bundle;
    import android.util.Log;
    
    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.AMapLocationListener;
    import com.unity3d.player.UnityPlayerActivity;;
    
    public class MainActivity extends UnityPlayerActivity  {
    
        //声明AMapLocationClient类对象
        public AMapLocationClient mLocationClient = null;
        //声明AMapLocationClientOption对象
        public AMapLocationClientOption mLocationOption = null;
        //定位回调字符串
        private String LocationInfo = "Init String~";
    
        @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(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("
    纬度:");
                        sb.append(location.getLatitude());
                        sb.append("
    纬度:");
                        sb.append(location.getLongitude());
                        sb.append("
    精度:");
                        sb.append(location.getAccuracy());
                        sb.append("
    地址:");
                        sb.append(location.getAddress());
                        sb.append("
    国家信息:");
                        sb.append(location.getCountry());
                        sb.append("
    省信息:");
                        sb.append(location.getProvince());
                        sb.append("
    城市信息:");
                        sb.append(location.getCity());
                        sb.append("
    城区信息:");
                        sb.append(location.getDistrict());
                        sb.append("
    街道信息:");
                        sb.append(location.getStreet());
                        sb.append("
    街道门牌号信息:");
                        sb.append(location.getStreetNum());
                        sb.append("
    城市编码:");
                        sb.append(location.getCityCode());
                        sb.append("
    地区编码:");
                        sb.append(location.getAdCode());
                        sb.append("
    定位点AOI信息:");
                        sb.append(location.getAoiName());
                        LocationInfo = sb.toString();
                    }
                    else 
                    {
                        mLocationClient.stopLocation();
                        mLocationClient.startLocation();
                        LocationInfo ="定位出现错误,"+location.getErrorCode()+","+location.getErrorInfo();
                    }
                }
                else
                {
                    LocationInfo="定位彻底失败";
                }
            }
        };
    }

    2.4AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.mirror.locationtest"
        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:icon="@drawable/ic_launcher"
                android:label="@string/app_name"
                android:theme="@style/AppTheme" >
                
                <service android:name="com.amap.api.location.APSService"></service>
    
                <activity
                    android:name="com.mirror.locationtest.MainActivity"
                    android:label="@string/app_name" >
                    <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="041cc66fb397b6f30cdb8683d24c7833">          
                </meta-data>
    
            </application>
    
        </manifest>

    2.5Eclipse选中我们的工程文件夹,可以先点击工具栏的Project->clean,再Project->Build Project。然后右键Export,选导出为JAR文件导出jar

    2.6Unity部分

    将安卓工程的文件拷贝到Unity工程的Plugins->Android文件夹里。没有这个目录的话需要自己创建一个。文件夹目录如下,把我们刚刚导出的JAR文件放在的bin文件夹中,另外还需要把我们的安卓工程目录里把libs文件夹和res文件夹以及AndroidManifest文件拷贝进去,注意拷贝完要把libs文件夹里面的classes.jar文件删掉,不然文件会冲突

       

    2.7代码GetLocationInfo.CS

    using UnityEngine;
    using UnityEngine.UI;
    
    public class GetLocationInfo : MonoBehaviour {
        public Text text;
        // Use this for initialization
        void Start()
        {
            InvokeRepeating("StartLocation", 1, 5);
        }
    
        public void StartLocation()
        {
            AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
            text.text = jo.Call<string>("GetInfo");
        }
    }

    2.8运行结果

    2.9Unity打包配置

    三根据返回错误代码判断错误

    3.1错误代码对照表

    http://lbs.amap.com/api/android-location-sdk/guide/utilities/errorcode

  • 相关阅读:
    软件:IIS上配置CGI
    腾讯RTX二次开发相关的一些注意事项
    ABP从入门到精通(3):aspnet-zero-core 使用Redis缓存
    ABP从入门到精通(2):aspnet-zero-core 使用MySql数据库
    ABP从入门到精通(1):aspnet-zero-core项目启动及各项目源码说明
    nodejs-ORM 操作数据库中间件waterline的使用
    nodejs-日志组件log4js的使用方法
    使用 Live555 搭建流媒体服务器
    如何在Windows资源管理器右键菜单中 添加CMD
    .NET 通过 Autofac 和 DynamicProxy 实现AOP
  • 原文地址:https://www.cnblogs.com/JimmyCode/p/7852618.html
Copyright © 2011-2022 走看看