美文网首页
Unity接入百度地图

Unity接入百度地图

作者: 90qq | 来源:发表于2018-10-09 14:16 被阅读0次

    我用的是Eclipse
    1.首先去百度地图官网注册一个密钥


    image.png image.png
    包名不一致的话会导致地图显示不出来,只显示网格
    https://blog.csdn.net/dashan618/article/details/72416865(这个网址是关于怎么获取SHA1的)

    2.利用eclipse接入百度地图SDK
    在项目中新建libs文件夹然后把以下文件放进去


    image.png

    (这些文件是从百度地图官网上下载的SDK中的)
    下面是各个文件中的代码
    主入口文件MainActivity.java

    import android.os.Bundle;
    import android.content.Intent;
    import com.unity3d.player.UnityPlayerActivity;
    
    public class MainActivity extends UnityPlayerActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
        
        public String getStringInJava() {
            return "i am a string from java";
        }
        
        public void OpenNewScene() {
            Intent intent = new Intent(MainActivity.this, EmptyScene.class);
            this.startActivity(intent);
        }
        
        public void OpenBaiduMap() {
            Intent intent = new Intent(MainActivity.this, MyBaiduMapActivity.class);
            this.startActivity(intent);
        }
    }
    

    EmptyScene.java

    import android.app.Activity;
    import android.os.Bundle;
    
    public class EmptyScene extends Activity{
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    }
    

    MyBaiduMapActivity.java

    import com.baidu.mapapi.CoordType;
    import com.baidu.mapapi.SDKInitializer;
    import com.baidu.mapapi.map.MapView;
     
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MyBaiduMapActivity extends Activity {
    
        MapView mMapView = null;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {                
            super.onCreate(savedInstanceState);             
            SDKInitializer.initialize(getApplicationContext());     
            SDKInitializer.setCoordType(CoordType.BD09LL);
            setContentView(R.layout.activity_main);                
            mMapView = (MapView) findViewById(R.id.bmapView);   
        } 
        
        @Override
        protected void onDestroy() {        
            super.onDestroy();          
            //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理          
            mMapView.onDestroy();    
        }
    
        @Override    
        protected void onResume() {        
            super.onResume();        
            //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理         
            mMapView.onResume();    
        }    
        @Override      
        protected void onPause() {          
            super.onPause();        
            //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理         
            mMapView.onPause();     
        }
    
    }
    

    布局文件Activity_main.xml 添加以下代码

    <com.baidu.mapapi.map.MapView  
        android:id="@+id/bmapView"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:clickable="true" />
    

    主配置文件AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.qingfeng.baidumap"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="21" />
    
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
        //获取设备网络状态,禁用后无法获取网络状态
        <uses-permission android:name="android.permission.INTERNET"/> 
        //网络权限,当禁用后,无法进行检索等相关业务
        <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
        //读取设备硬件信息,统计数据
        <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> 
        //读取系统信息,包含系统版本等信息,用作统计
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
        //获取设备的网络状态,鉴权所需网络代理
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
        //允许sd卡写权限,需写入地图数据,禁用后无法显示地图
        <uses-permission android:name="android.permission.WRITE_SETTINGS" /> 
        //获取统计数据
        <uses-permission android:name="android.permission.CAMERA" />  
        //使用步行AR导航,配置Camera权限
      
       
       <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> </service>
        
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            
            <meta-data  
            android:name="com.baidu.lbsapi.API_KEY"  
            android:value="pz0BR5Gng2mPnZoQojFWVnKcU07iGvOB" />  
            
            <activity android:label="@string/app_name" 
                android:screenOrientation="fullSensor" 
                android:launchMode="singleTask" 
                android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale" 
                android:name="com.qingfeng.baidumap.MainActivity">      
                <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>    
                  
                <activity android:name="com.qingfeng.baidumap.EmptyScene" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">    
                </activity>     
                   
                <activity android:name="com.qingfeng.baidumap.MyBaiduMapActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">    
                </activity>
    
        </application>
    
    </manifest>
    
    

    3.导出文件给Unity


    image.png

    记得把libs里面的classes删除


    image.png

    assets文件夹在百度地图的demo找,据说不加这个程序会崩溃
    Demo\BaiduMap_AndroidSDK_v5.2.0_Sample\BaiduMapsApiDemo

    4.编写C#脚本,实现交互

    void OnGUI() {
            if (GUILayout.Button("Open Map", GUILayout.Height(100))) {
                AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
                jo.Call("OpenBaiduMap");
            }
    
            if (GUILayout.Button("Open New Scene", GUILayout.Height(100))) {
                AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
                jo.Call("OpenNewScene");
            }
    
            if (GUILayout.Button("改变Label___New", GUILayout.Height(100))) {
                AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
                text.text = jo.Call<string>("getStringInJava");
            }
        }
    

    相关文章

      网友评论

          本文标题:Unity接入百度地图

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