2016年12月4日 星期日

Android-GoogleMap 地圖開發

此篇為簡略流程,可以先參考,尚未整理。

想必隨著應用程式的多元
越來越多創新的應用程式也在 GoogleMap 添加新的色彩
例如 : 寶可夢 !!!

 這篇就教大家如何在專案內添加 GoogleMap
 並且能夠執行
首先在我們專案 Gradle 添加 Google Service
compile 'com.google.android.gms:play-services:7.0.0'

並且添加網路以及 GPS 相關權限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

再來記得去 : Google API 申請鑰匙

開啟專案底下 res -> values ->  google_maps_api.xml 新增
並且將您的 Key 放入指定位置內
以下是官方文件
<resources>
    <!--
    TODO: Before you run your application, you need a Google Maps API key.
    See this page for more information:
    https://developers.google.com/maps/documentation/android/start#get_an_android_certificate_and_the_google_maps_api_key
    Once you have your key (it starts with "AIza"), replace the "google_maps_key"
    string in this file.
    Note: This resource is used for the release build target. You likely only want to update the string
    in the debug/.../google_maps_api.xml instead if you just want to run this app.
    (This file is used for the release target that uses the release key store.)
    -->
    <!-- translatable 可以轉換任意語言 -->
    <string name="google_maps_key" translatable="false" templateMergeStrategy="preserve">
        YourApiKey
    </string>
</resources>

把版本代號以及 API Key放置對應位置
<application
    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.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />

    .
    .
    .
</application>


建立 XML 介面
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context=".activity.MapActivity" />

Activity 內容
package com.net.lightblue.gototaipei.activity;

import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapActivity extends Activity 
                implements OnMapReadyCallback {

    private SupportMapFragment mapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(map);
        mapFragment.getMapAsync(this);

    }


    @Override
    public void onMapReady(GoogleMap map) {
        // Add a marker in Sydney, Australia, and move the camera.mera.
        LatLng sydney = new LatLng(-34, 151);
        map.addMarker(new MarkerOptions().position(sydney).title("Hello"));
        map.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

}

參考資料 : Maps Android API 
參考資料 : GoogleMap 相機檢視
歡迎轉載,請註明出處。

沒有留言:

張貼留言