การใช้ Google Maps API และ Location Services ใน Android
ตัวอย่างนี้สาธิตการแสดงแผนที่ Google Maps และวาง Marker ที่ตำแหน่งที่กำหนด (Hardcoded) พร้อมขอสิทธิ์เข้าถึงตำแหน่งที่ตั้ง
ขั้นตอนที่ 1: สร้างโปรเจกต์และเพิ่ม Dependency
สร้างโปรเจกต์ใหม่ เลือก Template "Google Maps Activity" หรือเพิ่ม dependency:
implementation 'com.google.android.gms:play-services-maps:18.2.0' implementation 'com.google.android.gms:play-services-location:21.0.1'
ขั้นตอนที่ 2: Layout (activity_maps.xml)
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
ขั้นตอนที่ 3: โค้ดหลัก (MapsActivity.java)
import androidx.fragment.app.FragmentActivity;
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 MapsActivity extends FragmentActivity implements OnMapReadyCallback {
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// ตำแหน่งตัวอย่าง: กรุงเทพมหานคร
LatLng myCurrentLocation = new LatLng(13.0827, 80.2707);
mMap.addMarker(new MarkerOptions().position(myCurrentLocation).title("This is My current Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myCurrentLocation, 15));
}
}
ขั้นตอนที่ 4: กำหนด API Key (res/values/strings.xml)
<resources>
<string name="app_name">Sample</string>
<string name="title_activity_maps">Map</string>
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_API_KEY_HERE</string>
</resources>
ขั้นตอนที่ 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
🔑 วิธีขอ Google Maps API Key
- ไปที่ Google Cloud Console
- สร้างโปรเจกต์ใหม่หรือเลือกโปรเจกต์เดิม
- เปิดใช้งาน Maps SDK for Android
- สร้าง Credentials → API Key
- จำกัด API Key ให้ใช้ได้เฉพาะแอปของคุณ (Package name + SHA-1)
- นำ API Key ไปใส่ใน
strings.xml
📍 การดึงตำแหน่งจริง (Real-time Location)
โค้ดตัวอย่างใช้ตำแหน่ง Hardcoded สำหรับตำแหน่งจริงควรใช้ FusedLocationProviderClient:
FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, location -> {
if (location != null) {
LatLng current = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(current).title("My Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(current, 15));
}
});
การทดสอบ
รันแอปบนอุปกรณ์จริง (ต้องมี Google Play Services) จะแสดงแผนที่พร้อม Marker ตำแหน่งที่กำหนด