Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android開發學習之路--百度地圖之初體驗

Android開發學習之路--百度地圖之初體驗

編輯:關於android開發

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布局:
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E-->
<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">

</com.baidu.mapapi.map.mapview></textview></linearlayout>

接著修改權限,修改AndroidManifest文件如下:
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E-->
<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">
    <!--{cke_protected}{C}%3C!%2D%2D%20%E6%9D%A5%E7%94%B5%E6%B6%88%E9%9F%B3%20%2D%2D%3E-->
    <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">
            </category></action></intent-filter>
        </activity>
    </meta-data></application>


</uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></permission></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 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接口設置位置,控制縮放了。這裡添加了一個小圈圈表示位置,運行結果如下:

\

如圖已經很明顯地顯示了當前的位置。

百度地圖還有好多的內容可以學習,這裡是初體驗,就簡單學習這些了。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved