• Android开发学习之路--百度地图之初体验


        手机都有gps和网络,通过gps或者网络可以定位到自己,然后通过百度,腾讯啊之类的地图可以显示我们的地理位置。这里学习下百度地图的使用。首先就是要申请开发者了,这个详细就不多讲了。http://developer.baidu.com/map/wiki/index.php?title=androidsdk/guide/key。可以参考这里,详细介绍了怎么申请。

        然后创建应用,创建好了之后,如下所示:


        接着是下载百度的sdk了。http://lbsyun.baidu.com/index.php?title=androidsdk/sdkandev-download。一键下载即可,下载好后是v3.7.1版本,如下:


        之后需要把libs下的内容拷贝到项目中的。接着我们新建工程,emBaiduMapStudy。然后把上面libs下的jar包和文件夹下的so都拷贝到项目的libs目录下。修改build.gradle:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
    
        defaultConfig {
            applicationId "com.jared.embaidumapstudy"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        sourceSets {
            main {
                jniLibs.srcDirs = ['libs']
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.1.1'
    }
    

        这里添加了sourceSets。接着我们修改layout布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_margin="5dp">
    
        <TextView
            android:text="Hello Baidu Map"
            android:layout_gravity="center"
            android:layout_margin="2dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <com.baidu.mapapi.map.MapView
            android:id="@+id/bmapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"/>
    
    </LinearLayout>
    

        接着修改权限,修改AndroidManifest文件如下:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.jared.embaidumapstudy" >
    
        <permission android:name="android.permission.BAIDU_LOCATION_SERVICE" />
    
        <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="android.permission.ACCESS_GPS" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.CALL_PHONE" />
        <uses-permission android:name="android.permission.READ_SMS" />
        <uses-permission android:name="android.permission.SEND_SMS" />
        <uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <uses-permission android:name="android.permission.BROADCAST_STICKY" />
        <uses-permission android:name="android.permission.WRITE_SETTINGS" />
        <!-- 来电消音 -->
        <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    
        <application
            android:name=".DemoApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme" >
    
            <meta-data
                android:name="com.baidu.lbsapi.API_KEY"
                android:value="lMYt4e8sq4jPE6GMGLwtra4z" />
    
            <activity android:name=".MainActivity" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    
    </manifest>
    

        这里有一个比较恶心的,需要application的android:name属性。接着是各种权限,还有得加上meta-data的内容里的key,也就是上面申请的key了。因为有个android:name属性,所以需要新建类DemoApplication继承Application的。代码如下:

    package com.jared.embaidumapstudy;
    
    /**
     * Created by jared on 16/2/20.
     */
    
    import android.app.Application;
    import com.baidu.mapapi.SDKInitializer;
    
    public class DemoApplication extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
            SDKInitializer.initialize(this);
        }
    }
    

        然后修改MainActivity代码如下:

    package com.jared.embaidumapstudy;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    import com.baidu.mapapi.map.BaiduMap;
    import com.baidu.mapapi.map.MapView;
    
    public class MainActivity extends Activity {
    
        private MapView baiduMapView;
        private BaiduMap mBaiduMap;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
    
            baiduMapView = (MapView)findViewById(R.id.bmapView);
            mBaiduMap = baiduMapView.getMap();
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            baiduMapView.onDestroy();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            baiduMapView.onResume();
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            baiduMapView.onPause();
        }
    }
    

        运行下看下效果如下:


        折腾了好久终于把百度地图搞出来了。既然地图有了,那接下来就是要定位了。下面来实现下定位的功能和自定义缩放。修改代码如下:

    package com.jared.embaidumapstudy;
    
    import android.app.Activity;
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;
    
    import com.baidu.mapapi.map.BaiduMap;
    import com.baidu.mapapi.map.BitmapDescriptor;
    import com.baidu.mapapi.map.BitmapDescriptorFactory;
    import com.baidu.mapapi.map.MapStatusUpdate;
    import com.baidu.mapapi.map.MapStatusUpdateFactory;
    import com.baidu.mapapi.map.MapView;
    import com.baidu.mapapi.map.MyLocationConfiguration;
    import com.baidu.mapapi.map.MyLocationData;
    
    import java.util.List;
    
    public class MainActivity extends Activity {
    
        private MapView baiduMapView;
        private BaiduMap mBaiduMap;
    
        private LocationManager locationManager;
        private String provider;
        BitmapDescriptor mCurrentMarker;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
    
            baiduMapView = (MapView)findViewById(R.id.bmapView);
            mBaiduMap = baiduMapView.getMap();
    
            locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            List<String> providerList = locationManager.getProviders(true);
            if(providerList.contains(LocationManager.GPS_PROVIDER)) {
                provider = LocationManager.GPS_PROVIDER;
            } else if(providerList.contains(LocationManager.NETWORK_PROVIDER)) {
                provider = LocationManager.NETWORK_PROVIDER;
            } else {
                Toast.makeText(getApplicationContext(), "No location provider to use",
                        Toast.LENGTH_LONG).show();
                return;
            }
            provider = LocationManager.NETWORK_PROVIDER;
            Location location = locationManager.getLastKnownLocation(provider);
            if(location != null) {
                navigateTo(location);
            }
        }
    
        private void navigateTo(Location location) {
            // 开启定位图层
            mBaiduMap.setMyLocationEnabled(true);
            //.accuracy(location.getRadius())
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getAccuracy())
                    // 此处设置开发者获取到的方向信息,顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            Log.d("Location", String.valueOf(location.getLatitude())+":"
                    +String.valueOf(location.getLongitude()));
            // 设置定位数据
            mBaiduMap.setMyLocationData(locData);
            // 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标)
            mCurrentMarker = BitmapDescriptorFactory
                    .fromResource(R.drawable.icon_geo);
            MyLocationConfiguration config = new MyLocationConfiguration(
                    MyLocationConfiguration.LocationMode.FOLLOWING, true, mCurrentMarker);
            mBaiduMap.setMyLocationConfigeration(config);
    
            perfomZoom(15);
        }
    
        private void perfomZoom(int zoom) {
            float zoomLevel = Float.parseFloat(String.valueOf(zoom));
            MapStatusUpdate u = MapStatusUpdateFactory.zoomTo(zoomLevel);
            mBaiduMap.animateMapStatus(u);
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            baiduMapView.onDestroy();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            baiduMapView.onResume();
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            baiduMapView.onPause();
        }
    }
    

        这里通过Location来获取位置,因为室内gps信号很弱,就用网络的位置了,接着就是用了BaiduMap接口设置位置,控制缩放了。这里添加了一个小圈圈表示位置,运行结果如下:


        如图已经很明显地显示了当前的位置。

        百度地图还有好多的内容可以学习,这里是初体验,就简单学习这些了。

  • 相关阅读:
    synchronized关键字jvm实现及各种锁
    zookeeper选举算法
    git远程操作
    git分支的创建与分支之间合并的底层原理
    员工贷项目总结
    Python之字符串操作
    Python实现购物车的功能
    Python列表学习笔记
    Python中如何使用boolean类型的数据
    Python实现用户登录账户
  • 原文地址:https://www.cnblogs.com/wuyida/p/6299952.html
Copyright © 2020-2023  润新知