码迷,mamicode.com
首页 > 编程语言 > 详细

Unity高德LBS

时间:2017-11-17 18:24:46      阅读:771      评论:0      收藏:0      [点我收藏+]

标签:oid   tao   gettime   ken   ips   public   iat   res   cal   

一、准备

高德开发者页面注册产品 

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 Files\Unity17103\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Development\Classes

技术分享图片

 

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("\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 
                {
                    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

 

Unity高德LBS

标签:oid   tao   gettime   ken   ips   public   iat   res   cal   

原文地址:http://www.cnblogs.com/JimmyCode/p/7852618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!