Google Map หัดทำกัน!

สวัสดีครับ วันนี้เราจะมาแนะนำวิธีการสร้าง Map Activity กันนะครับ ว่ามีหลักการและเทคนิคในการทำอย่างไร เรามาเลยกันเลยครับ หากเราไม่ New Project ใหม่ แล้วเลือก Map Activity หรือถ้าเรามี Project อยู่แล้วก็ทำการ ไปที่ app->New->Google->Google Maps Activity
ไฟล์ที่จะถูกสร้างขึ้น
1. java/MapsActivity.java
2. layout/activity_maps.xml
3. values/google_maps_api.xml
ถ้าพร้อมแล้วเรามาเริ่มโค้ดกันเลย!
เปิดไฟล์ manifests/AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.todo.geekcreator.walk">
    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->

    <permission
        android:name="com.todo.geekcreator.walk.permission.Maps_RECEIVE"
        android:protectionLevel="signature" />
    
    <uses-permission android:name="com.todo.geekcreator.walk.permission.Maps_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="com.google.provides.gsf.permission.READ_GSERVICES" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

เปิดไฟล์ values/google_maps_api.xml
https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=..........com.todo.geekcreator.walk
copy link แล้วนำไปว่างที่ browser ฮ่าๆๆ ละเอียดไปเอาเป็นว่าก็อปลิงค์นี้เพื่อเอา Key ของ Google map แล้วสร้างคีย์ API
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyDiY-xLMjziFOnHmGWd4vz29dmnqp...</string>

แล้วนำคีย์ที่ได้มาสร้างในครับ เพียงเท่านี้เราก็ได้ Key สำหรับ debug แล้วครับ หากเราต้องการที่จะอัพขึ้น play store ก็
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyDiY-xLMjziFOnHmGWd4vz29dmnqp...</string>

ไปไว้ใน values/strings.xml เพียงเท่านี้ก็สามารถใช้ map ได้แล้วครับ สำหรับวันนี้ก็ต้องลาไปก่อน หากมีข้อสงสัยสามารถคอมเมนต์ได้ที่โพสต์ด้านล่างเลยครับ

ความคิดเห็น

บทความที่ได้รับความนิยม