【Unity】unity3d 获取手机地理位置

发表于2017-08-24
评论0 4.2k浏览

怎么用Unity3D 获取手机地理位置呢?unity的某些场景中,如果需要定位地理位置的话,但是unity却没有提供相应的接口。对于安卓(这里举例,iOS暂没研究)这个时候只能用Java写个获取地理位置的方法,然后与unity交互。这样就能在unity脚本中获取当前手机的地理位置。 实际开发时,为了方便我直接调用百度的API来获取地理信息。 获取地理位置,有三种方式: 

   1、手机信号塔数据:当移动设备的GPS芯片不能接收到GPS信号时,移动设备就需要与它所连接的手机信号塔通讯和估算它与信号塔之间的距离以不断报告它的地理位置。通过这种方法获得的地理位置数据不如纯粹的GPS数据精确。

     2、WiFi连接:这是一种能够精确获得地理位置数据的方法,但是需要用到有效的WiFi热点。WiFi的地址与GPS坐标是一一对应的。它可以准确地标示出用户所处的位置,因此很多零售商才愿意提供免费的公共WiFi服务,那样它们就能够发布店内移动广告了。

     3、IP地址:地理位置也可以通过与数据连接有关的IP地址来获得。这种方法的准确性会因运营商而异,可靠性也比上述方法要低一些。

     4、GPS:精确,无需连接网络,但是连接时比较耗时。。

    用百度API调用时,首先需要申请一个key。具体的可以去官网试试。根据IP地址调用,比较方便,因为可以直接用http的方式发送请求,然后返回json格式的数据,如下:


    [csharp] view plain copy
    1. public class AddressTool{  
    2.                 //ip为空,默认为本机ip  
    3.                 public static IEnumerator GetAddressFromIP(string ipAddress, AddressForQueryIPFromBaidu data)  
    4.                 {  
    5.                         string baiduKey = "42c43828c02a9f6144dd305643ca73b8";  
    6.                         //ip:ip地址(可选,ip不出现,或者出现且为空字符串的情况下,会使用当前访问者的IP地址作为定位参数),ak:用户密钥,  
    7.                     //coor:输出的坐标格式(可选,coor不出现时,默认为百度墨卡托坐标;coor=bd09ll时,返回为百度经纬度坐标)   
    8.                         string url = "http://api.map.baidu.com/location/ip?ak=" baiduKey "&coor=bd09ll&ip=202.198.16.3";  
    9.                         WWW w = new WWW(url);  
    10.                         //超时时间  
    11.                         float t = Time.time;  
    12.                         while(!w.isDone){  
    13.                                 if(Time.time - t > 15f){  
    14.                                         Debug.Log("server time out!");  
    15.                                         w.Dispose();  
    16.                                         yield break;  
    17.                                 }  
    18.                                 yield return null;  
    19.                         }  
    20.                         //接收http请求返回的数据(json格式)  
    21.                         string jsonData = w.text;  
    22.             //解析成对象  
    23.                         AddressForQueryIPFromBaidu result =  (AddressForQueryIPFromBaidu)ValueParse.ParseJsonToClass(typeof(AddressForQueryIPFromBaidu), jsonData);  
    24.                 }  
    25.         }  
    26.         #region AddressForQueryIPFromBaidu  
    27.         public class AddressForQueryIPFromBaidu  
    28.         {  
    29.                 //地址   
    30.                 public string address { getset; }  
    31.                 //详细内容    
    32.                 public Content content { getset; }  
    33.                 //返回状态码  
    34.                 public string status { getset; }  
    35.    
    36. //                public string ToString(){  
    37. //                        return string.Format("根据IP获取地理位置 地址:{0}, 详细内容:{1},返回状态码:{2}",address.ToString(),content,status);  
    38. //                }  
    39.         }  
    40.         public class Content  
    41.         {  
    42.                 //简要地址   
    43.                 public string address { getset; }  
    44.                 //详细地址信息  
    45.                 public Address_Detail address_Detail { getset; }  
    46.                 //百度经纬度坐标值    
    47.                 public Point point { getset; }  
    48.         }  
    49.         public class Address_Detail  
    50.         {  
    51.                 //城市    
    52.                 public string city { getset; }  
    53.                 //百度城市代码    
    54.                 public string city_Code { getset; }  
    55.                 //区县   
    56.                 public string district { getset; }  
    57.                 //省份  
    58.                 public string province { getset; }  
    59.                 //街道  
    60.                 public string street { getset; }  
    61.                 //门址  
    62.                 public string street_Number { getset; }  
    63.         }  
    64.         public class Point  
    65.         {  
    66.                 public string x { getset; }  
    67.                 public string y { getset; }  
    68.         }  


    IP定位的结果精度较差,主要应用获取省份或者城市的位置信息。百度Android定位SDK是利用设备当前的GPS信息(GPS定位),网络信息(基站定位 和 Wi-Fi定位)完成定位的。开发者在应用中成功集成百度定位SDK(包括一个armeabi的文件夹也得放在libs中)以后,既可以方便的通过定位SDK的接口向百度定位服务请求位置信息。下载百度定位的SDK,导入到安卓工程中。相关android代码,如下:

    [csharp] view plain copy
    1. package com.zs.address;  
    2. import org.json.JSONException;  
    3. import org.json.JSONObject;  
    4. import android.annotation.SuppressLint;  
    5. import android.app.Activity;  
    6. import android.os.Bundle;  
    7. import android.util.Log;  
    8. import android.view.Menu;  
    9. import android.view.View;  
    10. import android.widget.Button;  
    11. import android.widget.TextView;  
    12. import com.baidu.location.BDLocation;  
    13. import com.baidu.location.BDLocationListener;  
    14. import com.baidu.location.LocationClient;  
    15. import com.baidu.location.LocationClientOption;  
    16. import com.baidu.location.LocationClientOption.LocationMode;  
    17. import com.unity3d.player.UnityPlayer;  
    18. import com.unity3d.player.UnityPlayerActivity;  
    19.    
    20. @SuppressLint("NewApi")  
    21. public class MainActivity extends UnityPlayerActivity{  
    22.         public LocationClient mLocationClient = null;  
    23.         public BDLocationListener mListener = new MyLocationListener();  
    24.    
    25. //        private TextView text;  
    26. //        private Button againBtn, clearBtn, startBtn, stopBtn;  
    27.         @Override  
    28.         protected void onCreate(Bundle savedInstanceState) {  
    29.                 super.onCreate(savedInstanceState);  
    30.                 //setContentView(R.layout.activity_main);  
    31.                 //initView();  
    32.                 //初始化百度定位SDK  
    33.                 initLocationService();  
    34.         }  
    35.            
    36.         @SuppressLint("NewApi")  
    37.         public void initLocationService() {  
    38.                 // 声明LocationClient类  
    39.                 mLocationClient = new LocationClient(getApplicationContext());  
    40.                 mLocationClient.registerLocationListener(mListener);//注册监听函数  
    41.                    
    42.                 LocationClientOption option = new LocationClientOption();  
    43.                 option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式  
    44.                 option.setCoorType("gcj02");// 返回的定位结果,默认值gcj02  
    45.                 option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms  
    46.                 option.setIsNeedAddress(true);// 返回的定位结果包含地址信息  
    47.                 option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向  
    48.                 option.setOpenGps(true);//  
    49.                 mLocationClient.setLocOption(option);  
    50.                 mLocationClient.requestLocation();  
    51.         }  
    52.            
    53.         //启动定位服务,给unity调用  
    54.         public void startLocationService(){  
    55.                 mLocationClient.start();  
    56.         }  
    57.         //停止定位服务,给unity调用  
    58.         public void stopLocationService(){  
    59.                 mLocationClient.stop();  
    60.         }  
    61.    
    62.         class MyLocationListener implements BDLocationListener{  
    63.                 @Override  
    64.                 public void onReceiveLocation(BDLocation location) {  
    65.                         // public int getLocType ( )返回值:  
    66.                         // 61 : GPS定位结果  
    67.                         // 62 : 扫描整合定位依据失败。此时定位结果无效。  
    68.                         // 63 : 网络异常,没有成功向服务器发起请求。此时定位结果无效。  
    69.                         // 65 : 定位缓存的结果。  
    70.                         // 66 : 离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果  
    71.                         // 67 : 离线定位失败。通过requestOfflineLocaiton调用时对应的返回结果  
    72.                         // 68 : 网络连接失败时,查找本地离线定位时对应的返回结果  
    73.                         // 161: 表示网络定位结果  
    74.                         // 162~167: 服务端定位失败  
    75.                         // 502:key参数错误  
    76.                         // 505:key不存在或者非法  
    77.                         // 601:key服务被开发者自己禁用  
    78.                         // 602:key mcode不匹配  
    79.                         // 501~700:key验证失败  
    80.                         if (location == null)  
    81.                                 return;  
    82.                         JSONObject jo = new JSONObject();  
    83.                         try {  
    84.                                 jo.put("error_code", location.getLocType());  
    85.                                 jo.put("time", location.getTime());  
    86.                                 jo.put("latitude", location.getLatitude());//获取维度  
    87.                                 jo.put("lontitude", location.getLongitude());//获取经度  
    88.                                 jo.put("radius", location.getRadius());  
    89.                                 jo.put("direction", location.getDirection());  
    90.                                 //只有使用GPS定位的情况下,获取移动速度,当前连接的卫星编号  
    91.                                 if(location.getLocType() == BDLocation.TypeGpsLocation){  
    92.                                         JSONObject gpsInfo = new JSONObject();  
    93.                                         gpsInfo.put("speed", location.getSpeed());  
    94.                                         gpsInfo.put("satellite", location.getSatelliteNumber());  
    95.                                         jo.put("gpsInfo", gpsInfo);  
    96.                                 }else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {  
    97.                                         // 只有使用网络定位的情况下,才能获取当前位置的反地理编码描述。  
    98.                                         // 自定位SDK2.6版本之后,支持获取省/市/区分级地理信息:  
    99.                                         JSONObject netWorkInfo = new JSONObject();  
    100.                                         netWorkInfo.put("address", location.getAddrStr());//获取反地理编码  
    101.                                         netWorkInfo.put("province", location.getProvince());//获取省份信息  
    102.                                         netWorkInfo.put("city", location.getCity());//获取城市信息  
    103.                                         netWorkInfo.put("city_code", location.getCityCode()); //获取城市编码  
    104.                                         netWorkInfo.put("district", location.getDistrict()); //获取区县信息  
    105.                                         netWorkInfo.put("street", location.getStreet()); //获取街道地址  
    106.                                         netWorkInfo.put("street_number", location.getStreetNumber()); //获取街道门牌号  
    107.                                         jo.put("netWorkInfo", netWorkInfo);  
    108.                                 }  
    109.                         } catch (JSONException e1) {  
    110.                                 Log.i("address", e1.toString());  
    111.                         }  
    112.                            
    113.                         Log.i("address", jo.toString());  
    114.                         //将获取的地理信息返回给unity  
    115.                         UnityPlayer.UnitySendMessage("address""AddressCallback", jo.toString());  
    116.                         //text.setText(jo.toString());  
    117.                 }  
    118.         }  
    119.            
    120.         private void requestLocation() {  
    121.                 mLocationClient.requestLocation();  
    122.         }  
    123.         @Override  
    124.         public boolean onCreateOptionsMenu(Menu menu) {  
    125.                 // Inflate the menu; this adds items to the action bar if it is present.  
    126.                 getMenuInflater().inflate(R.menu.main, menu);  
    127.                 return true;  
    128.         }  
    129.    
    130. //        private void initView(){  
    131. //                text = (TextView)findViewById(R.id.textView);  
    132. //                againBtn = (Button)findViewById(R.id.button1);  
    133. //                clearBtn = (Button)findViewById(R.id.button2);  
    134. //                startBtn = (Button)findViewById(R.id.button3);  
    135. //                stopBtn = (Button)findViewById(R.id.button4);  
    136. //                againBtn.setOnClickListener(clickListener);  
    137. //                clearBtn.setOnClickListener(clickListener);  
    138. //                startBtn.setOnClickListener(clickListener);  
    139. //                stopBtn.setOnClickListener(clickListener);  
    140. //        }  
    141. //          
    142. //        private View.OnClickListener clickListener = new View.OnClickListener() {  
    143. //                public void onClick(View v) {  
    144. //                        switch (v.getId()) {  
    145. //                        case R.id.button1:  
    146. //                                requestLocation();  
    147. //                                break;  
    148. //                        case R.id.button2:  
    149. //                                text.setText("");  
    150. //                                break;  
    151. //                        case R.id.button3:  
    152. //                                startLocationService();  
    153. //                                break;  
    154. //                        case R.id.button4:  
    155. //                                stopLocationService();  
    156. //                                break;  
    157. //                        default:  
    158. //                                break;  
    159. //                        }  
    160. //                }  
    161. //        };  
    162. }  

    设置AndroidManifest.xml
    在application标签中声明service组件,这是百度SDK的定位service

    [csharp] view plain copy
    1. <service android:name=< span="">"com.baidu.location.f" android:enabled="true" android:process=":remote">  </service android:name=<>
    2.   
    声明使用权限

    [csharp] view plain copy
    1.   
    2. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"</uses-permission android:name=>  
    3.   
    4. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"</uses-permission android:name=>  
    5.   
    6. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"</uses-permission android:name=>  
    7.   
    8. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"</uses-permission android:name=>  
    9.   
    10. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"</uses-permission android:name=>  
    11.   
    12. <uses-permission android:name="android.permission.READ_PHONE_STATE"</uses-permission android:name=>  
    13.   
    14. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"</uses-permission android:name=>  
    15.   
    16. <uses-permission android:name="android.permission.INTERNET"</uses-permission android:name= />  
    17.   
    18. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"</uses-permission android:name=>  
    19.   
    20. <uses-permission android:name="android.permission.READ_LOGS"</uses-permission android:name=>  

    把MainActivity的一些注释打开,然后继承Activity,就可以直接做成app在手机上运行了。该项目demo,在本文最后。

           接下来就是把该安卓项目的代码打成jar包,然后放在unity项目资源下的Plugins->Android目录下。再把libs(里面只能放百度的第三方jar包,不要把unity的class.jar,和Android的jar包放在里面)、res文件夹、AndroidManifest,也放在Android目录下。在场景中放上一个空物体,名为‘address’(必须和java代码中“UnityPlayer.UnitySendMessage("address", "AddressCallback", jo.toString());”保持一致),编写一个脚本“AddressTest.cs”,绑在该空对象上面。,该脚本如下:

    [csharp] view plain copy
    1. using UnityEngine;  
    2. using System.Collections;  
    3. using address;  
    4. public class AddressTest : MonoBehaviour {  
    5.         //地理信息   
    6.         private string geography = "lala haha wawa";  
    7.         private GUIStyle style = new GUIStyle();  
    8.    
    9.         private int count = 0;  
    10.         private AndroidJavaObject jo;  
    11.    
    12.         public void Awake(){  
    13.                 //GUI样式   
    14.                 style.fontSize = 30;  
    15.                 style.wordWrap = true;  
    16.                 //初始化安卓的MainActivity。下面两句话是固定的,不能变动  
    17.                 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");  
    18.                 jo = jc.GetStatic("currentActivity");  
    19.         }  
    20.    
    21.         //回调函数,接收到地理信息,展示在页面上。   
    22.         public void AddressCallback(string jsonStr){  
    23.                 Debug.Log("zs : " jsonStr);  
    24.                 geography = "第"  ( count).ToString() "次接受信息:"   jsonStr;  
    25.         }  
    26.    
    27.         void OnGUI(){  
    28.                 GUI.Label(new Rect(0,0,700,300),geography,style);  
    29.                 //启动地理服务  
    30.                 if(GUI.Button(new Rect(0, 400,60,30)," 启 动 ")){  
    31.                         jo.Call("startLocationService");  
    32.                 }  
    33.                 //关闭地理服务  
    34.                 if(GUI.Button(new Rect(140, 400,60,30)," 停 止 ")){  
    35.                         jo.Call("stopLocationService");  
    36.                 }  
    37.         }  
    38. }  

    如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

    0个评论