请问怎样在pc端,把unity 高德地图插件交通地图导入unity

你正在使用的浏览器版本过低,将不能正常浏览和使用知乎。所有回答(2)
IP定位辅助 开启GPS的话 再利用gps修正 第一次加载其实是有误差的
第二次加载就修正了
之后每次都是读取cookie的参数 这样更精确就实现了
重要的是gps 没有gps其实大部分是有误差的
也说没有GPS
但是路由有mac 利用大数据 返回经纬度 也可以 再不行的就是基于ip的了
重要关键词 :GPS&大数据&经纬度&IP
浏览器获取的坐标是gps坐标,要先转换成高德的坐标再进行获取就能得到精确的位置了。
&&&您需要以后才能回答,未注册用户请先。4262人阅读
Unity3D(1)
Unity调用高德地图API,获取定位信息(Android)
先在Android端实现定位功能,再打包成JAR文件提供接口方法给Unity调用。
Unity3D (5.3.4)
Eclipse: Neon(4.6.0)
Unity提供了跟Android交互的接口,可以参考的博客。
1.首先新建一个Android工程,在最小SDK版本里选4.0以上,不然在Unity打包的时候会报错。
2.创建出来的工程目录如下:
3.可以删掉MainActivity里面不必要的代码,引用的布局也要删掉:
package com.example.
import android.app.A
import android.os.B
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
4.因为要调用Unity提供的接口,需要把Unity提供的JAR库classes.jar文件导入到我们的安卓工程里。我的Unity版本是5.3.4,文件在这里:
5.把库文件拖到Android工程的libs文件夹下面,右键选Build Path -&Add to Build Path导入包。
6.Activity中的方法想要被Unity调用,这个Activity就需要继承UnityPlayerActivity,这是Unity提供的包里面的接口类。代码里写了一个GetInfo()的方法给Unity在代码中调用,获取定位的信息LocationInfo。
package com.example.
import android.os.B
import com.unity3d.player.UnityPlayerA
public class MainActivity extends UnityPlayerActivity {
private String LocationI
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
public String GetInfo(){
return LocationI
7.接下来就是实现高德地图中的定位功能了,这方面的内容可以参考,还是很容易实现的,这里就不详述了。我的代码如下:
AndroidManifest.xml:
&?xml version="1.0" encoding="utf-8"?&
xmlns:android="/apk/res/android"
package="com.example.locationtest"
android:versionCode="1"
android:versionName="1.0" &
android:minSdkVersion="16"
android:targetSdkVersion="21" /&
android:name="android.permission.ACCESS_COARSE_LOCATION"&&
android:name="android.permission.ACCESS_FINE_LOCATION"&&
android:name="android.permission.ACCESS_NETWORK_STATE"&&
android:name="android.permission.ACCESS_WIFI_STATE"&&
android:name="android.permission.CHANGE_WIFI_STATE"&&
android:name="android.permission.INTERNET"&&
android:name="android.permission.READ_PHONE_STATE"&&
android:name="android.permission.WRITE_EXTERNAL_STORAGE"&&
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" &
android:name="com.amap.api.location.APSService"&&
android:name=".MainActivity"
android:label="@string/app_name" &
android:name="android.intent.action.MAIN" /&
android:name="android.intent.category.LAUNCHER" /&
android:name="com.amap.api.v2.apikey" android:value="ac239bfa4ad1bc32347f83"&
MainActivity.java:
package com.example.
import android.os.B
import android.util.L
import com.amap.api.location.AMapL
import com.amap.api.location.AMapLocationC
import com.amap.api.location.AMapLocationClientO
import com.amap.api.location.AMapLocationClientOption.AMapLocationM
import com.amap.api.location.AMapLocationL
import com.unity3d.player.UnityPlayerA;
public class MainActivity extends UnityPlayerActivity {
public AMapLocationClient mLocationClient = null;
public AMapLocationClientOption mLocationOption = null;
private String LocationI
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
public String GetInfo(){
startLocation();
return LocationI
protected void onStart() {
super.onStart();
private void startLocation(){
mLocationClient = new AMapLocationClient(getApplicationContext());
mLocationClient.setLocationListener(mLocationListener);
mLocationOption = new AMapLocationClientOption();
mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
mLocationOption.setInterval(2000);
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.startLocation();
public AMapLocationListener mLocationListener = new AMapLocationListener() {
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.e("AmapError","location Error, ErrCode:"
+ location.getErrorCode() + ", errInfo:"
+ location.getErrorInfo());
8.接下来就可以打包成JAR文件给Unity调用了。选中我们的工程文件夹,可以先点击工具栏的Project-&clean,再Project-&Build Project。然后右键Export,选导出为JAR文件,点击Next,在下一个窗口中展开工程目录,只需要导出src这个文件夹就可以了。点击Finish,导出JAR文件…
9.将安卓工程的文件拷贝到Unity工程的Plugins-&Android文件夹里。没有这个目录的话需要自己创建一个。文件夹目录如下,把我们刚刚导出的JAR文件放在的bin文件夹中,另外还需要把我们的安卓工程目录里把libs文件夹和res文件夹以及AndroidManifest文件拷贝进去,注意拷贝完要把libs文件夹里面的classes.jar文件删掉,不然文件会冲突:
10.接着就可以在Unity中调用了。注意Unity中的包名要和Android工程里的保持一致,否则会无法调用。我们新建一个简单的场景,添加一个Text和Button控件,在我们点击Button的时候,Text就会显示获取到的定位信息。
11.新建一个脚本,挂载在摄像机上面:
using UnityE
using System.C
using UnityEngine.UI;
public class GetLocationInfo : MonoBehaviour {
void Start () {
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");
在StartLocation()方法中通过jo.Call&返回值类型&(“方法名”)的方式调用了我们在Android工程里面提供的GetInfo方法。让Button的点击事件执行StartLocation()这个方法,即可获取定位信息。
用模拟器测试有些信息好像获取不到,但是真机测试是没问题的…
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:4536次
排名:千里之外5404人阅读
unity3d(46)
移动平台开发(52)
这里,用了一个比较偷懒的办法,直接用高德提供的android定位sdk,没有重新编译。好处是省事,坏处是,没法修改默认的定位模式。部分信息获取不到。
如果需要完整的功能,还是需要重新编译高德地图的sdk。
unity3d 5.3.2
高德android 定位 sdk 2.3
首先,注册高德sdk的用户,并且获取key。这里的key和编译环境的sha1相关。
新建一个unity项目
先把项目编译成android,记得Bundle Identifier必须和高德key里面的pakage一致。
用apk逆向工具把编译好的apk文件解开,
找到cert.rsa,用命令行获取调式用的sha1。官方只提供了android studio调式用的sha1获取的办法。
keytool -printcert -file cert.rsa
把下载的jar和逆相出来的AndroidManifest.xml导入到unity工程的Plugins/Android目录下
修改AndroidManifest.xml文件
&?xml version=&1.0& encoding=&utf-8& standalone=&no&?&
&manifest xmlns:android=&/apk/res/android& android:installLocation=&preferExternal& package=&com.nsh.gpsar& platformBuildVersionCode=&23& platformBuildVersionName=&6.0-2438415&&
&supports-screens android:anyDensity=&true& android:largeScreens=&true& android:normalScreens=&true& android:smallScreens=&true& android:xlargeScreens=&true&/&
&application android:banner=&@drawable/app_banner& android:debuggable=&false& android:icon=&@drawable/app_icon& android:isGame=&true& android:label=&@string/app_name& android:theme=&@style/UnityThemeSelector&&
&meta-data
android:name=&com.amap.api.v2.apikey&
android:value=&输入高德的key& /&
&!-- 定位需要的服务 --&
&service android:name=&com.amap.api.location.APSService& &&/service&
&activity android:configChanges=&locale|fontScale|keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode& android:label=&@string/app_name& android:launchMode=&singleTask& android:name=&com.unity3d.player.UnityPlayerActivity& android:screenOrientation=&portrait&&
&intent-filter&
&action android:name=&android.intent.action.MAIN&/&
&category android:name=&android.intent.category.LAUNCHER&/&
&category android:name=&android.intent.category.LEANBACK_LAUNCHER&/&
&/intent-filter&
&meta-data android:name=&unityplayer.UnityActivity& android:value=&true&/&
&/activity&
&/application&
&uses-feature android:glEsVersion=&0x20000&/&
&uses-feature android:name=&android.hardware.touchscreen& android:required=&false&/&
&uses-feature android:name=&android.hardware.touchscreen.multitouch& android:required=&false&/&
&uses-feature android:name=&android.hardware.touchscreen.multitouch.distinct& android:required=&false&/&
&uses-permission android:name=&android.permission.INTERNET& /&
&uses-permission android:name=&android.permission.WRITE_EXTERNAL_STORAGE& /&
&uses-permission android:name=&android.permission.ACCESS_COARSE_LOCATION& /&
&uses-permission android:name=&android.permission.ACCESS_NETWORK_STATE& /&
&uses-permission android:name=&android.permission.ACCESS_FINE_LOCATION& /&
&uses-permission android:name=&android.permission.READ_PHONE_STATE& /&
&uses-permission android:name=&android.permission.CHANGE_WIFI_STATE& /&
&uses-permission android:name=&android.permission.ACCESS_WIFI_STATE& /&
&uses-permission android:name=&android.permission.CHANGE_CONFIGURATION& /&
&uses-permission android:name=&android.permission.WAKE_LOCK& /&
&uses-permission android:name=&android.permission.WRITE_SETTINGS& /&
&/manifest&
做个简单的界面
添加类AmapEvent,将android的事件转化成unity的事件
using UnityE
using System.C
public class AmapEvent : AndroidJavaProxy {
public AmapEvent ()
: base (&com.amap.api.location.AMapLocationListener&)
void onLocationChanged (AndroidJavaObject amapLocation)
if (locationChanged != null) {
locationChanged (amapLocation);
public delegate void DelegateOnLocationChanged(AndroidJavaObject amap);
public event DelegateOnLocationChanged locationC
添加locationmange类,调用高德的sdk。定位时间和定位速度无法获取到,会报错。似乎在android studio里可以。
using UnityE
using System.C
using UnityEngine.UI;
public class LocationManage : MonoBehaviour
public Text txtL
public Text txtI
private AmapE
private AndroidJavaC
private AndroidJavaO
private AndroidJavaObject mLocationC
private AndroidJavaObject mLocationO
public void StartLocation ()
txtInfo.text = &start location...&;
txtInfo.text = txtInfo.text + &\r\n&;
jcu = new AndroidJavaClass (&com.unity3d.player.UnityPlayer&);
jou = jcu.GetStatic&AndroidJavaObject& (&currentActivity&);
txtInfo.text = txtInfo.text + &currentActivity get...&;
txtInfo.text = txtInfo.text + &\r\n&;
mLocationClient = new AndroidJavaObject (&com.amap.api.location.AMapLocationClient&, jou);
txtInfo.text = txtInfo.text + &AMapLocationClient get...&;
txtInfo.text = txtInfo.text + &\r\n&;
mLocationOption = new AndroidJavaObject (&com.amap.api.location.AMapLocationClientOption&);
txtInfo.text = txtInfo.text + &AMapLocationClientOption get...&;
txtInfo.text = txtInfo.text + &\r\n&;
mLocationClient.Call (&setLocationOption&, mLocationOption);
txtInfo.text = txtInfo.text + &setLocationOption...&;
amap = new AmapEvent ();
amap.locationChanged += OnLocationC
txtInfo.text = txtInfo.text + &\r\n&;
mLocationClient.Call (&setLocationListener&, amap);
txtInfo.text = txtInfo.text + &setLocationListener...&;
txtInfo.text = txtInfo.text + &\r\n&;
mLocationClient.Call (&startLocation&);
txtInfo.text = txtInfo.text + &startLocation...&;
} catch (Exception ex) {
txtInfo.text = txtInfo.text + &\r\n&;
txtInfo.text = txtInfo.text + &--------------------&;
txtInfo.text = txtInfo.text + ex.M
EndLocation ();
public void EndLocation ()
if (amap != null) {
amap.locationChanged -= OnLocationC
if (mLocationClient != null) {
mLocationClient.Call (&stopLocation&);
mLocationClient.Call (&onDestroy&);
txtLocation.text = &&;
private void OnLocationChanged (AndroidJavaObject amapLocation)
if (amapLocation != null) {
if (amapLocation.Call&int& (&getErrorCode&) == 0) {
txtLocation.text = &&&success:&;
txtLocation.text = txtLocation.text + &\r\n&&定位结果来源:& + amapLocation.Call&int& (&getLocationType&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&纬度:& + amapLocation.Call&double& (&getLatitude&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&经度:& + amapLocation.Call&double& (&getLongitude&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&精度信息:& + amapLocation.Call&float& (&getAccuracy&).ToString ();
//txtLocation.text = txtLocation.text + &\r\n&&定位时间:& + amapLocation.Call&AndroidJavaObject& (&getTime&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&地址:& + amapLocation.Call&string& (&getAddress&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&国家:& + amapLocation.Call&string& (&getCountry&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&省:& + amapLocation.Call&string& (&getProvince&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&城市:& + amapLocation.Call&string& (&getCity&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&城区:& + amapLocation.Call&string& (&getDistrict&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&街道:& + amapLocation.Call&string& (&getStreet&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&门牌:& + amapLocation.Call&string& (&getStreetNum&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&城市编码:& + amapLocation.Call&string& (&getCityCode&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&地区编码:& + amapLocation.Call&string& (&getAdCode&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&海拔:& + amapLocation.Call&double& (&getAltitude&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&方向角:& + amapLocation.Call&float& (&getBearing&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&定位信息描述:& + amapLocation.Call&string& (&getLocationDetail&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&兴趣点:& + amapLocation.Call&string& (&getPoiName&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&提供者:& + amapLocation.Call&string& (&getProvider&).ToString ();
txtLocation.text = txtLocation.text + &\r\n&&卫星数量:& + amapLocation.Call&int& (&getSatellites&).ToString ();
//txtLocation.text = txtLocation.text + &\r\n&&当前速度:& + amapLocation.Call&string& (&getSpeed&).ToString ();
} catch (Exception ex) {
txtLocation.text = txtLocation.text + &\r\n--------------ex-------------:&;
txtLocation.text = txtLocation.text + &\r\n& + ex.M
txtLocation.text = &&&amaperror:&;
txtLocation.text = txtLocation.text + &&&getErrorCode:& + amapLocation.Call&int& (&getErrorCode&).ToString ();
txtLocation.text = txtLocation.text + &&&getErrorInfo:& + amapLocation.Call&string& (&getErrorInfo&);
txtInfo.text = &amaplocation is null.&;
编译以后,运行,结果如下
源码及apk下载:http://download.csdn.net/detail/wuyt
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:527893次
积分:6099
积分:6099
排名:第3763名
原创:138篇
评论:158条
(1)(3)(8)(6)(2)(3)(2)(3)(4)(1)(3)(7)(11)(7)(3)(2)(1)(3)(2)(1)(1)(1)(1)(2)(4)(3)(2)(4)(2)(3)(3)(3)(3)(1)(7)(13)(2)(6)(8)}

我要回帖

更多关于 unity 高德 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信