Location Data

คุณลักษณะที่โดดเด่นอย่างหนึ่งของแอปพลิเคชันบนอุปกรณ์เคลื่อนที่คือการรับรู้ตำแหน่ง ผู้ใช้มือถือนำอุปกรณ์ของตนไปด้วยทุกที่ และเพิ่มตำแหน่ง การรับรู้ถึงแอปจะช่วยให้ผู้ใช้ได้รับประสบการณ์ตามบริบทมากขึ้น

ตัวอย่างโค้ด

ที่เก็บ ApiDemos บน GitHub ประกอบด้วย ตัวอย่างที่แสดงให้เห็นถึงการใช้ตำแหน่งบนแผนที่

Kotlin

Java

การทำงานกับข้อมูลตำแหน่ง

ข้อมูลตำแหน่งที่ใช้ได้กับอุปกรณ์ Android รวมถึงตำแหน่งปัจจุบัน ที่ระบุโดยใช้เทคโนโลยีต่างๆ ร่วมกัน ทิศทางและวิธีการเคลื่อนไหว รวมถึงว่าอุปกรณ์เคลื่อนที่ข้ามผ่านหรือไม่ ขอบเขตทางภูมิศาสตร์ที่กำหนดไว้ล่วงหน้า หรือเขตพื้นที่เสมือน ขึ้นอยู่กับความต้องการ แอปพลิเคชันของคุณ คุณสามารถเลือกระหว่างวิธีการต่างๆ ในการทำงานกับตำแหน่ง ข้อมูล:

  • เลเยอร์ตำแหน่งของฉัน มอบวิธีง่ายๆ ในการแสดงตำแหน่ง ตำแหน่งบนแผนที่ โดยไม่ได้ให้ข้อมูล
  • แนะนำให้ใช้ API ตำแหน่งของบริการ Google Play สำหรับทุกคน คำขอข้อมูลตำแหน่งแบบเป็นโปรแกรม
  • อินเทอร์เฟซ LocationSource ช่วยให้คุณระบุ ผู้ให้บริการตำแหน่งที่กำหนดเอง

สิทธิ์เข้าถึงตำแหน่ง

หากแอปของคุณต้องการเข้าถึงตําแหน่งของผู้ใช้ คุณต้องขอสิทธิ์โดยทำดังนี้ เพิ่มสิทธิ์เข้าถึงตำแหน่งที่เกี่ยวข้องของ Android ลงในแอป

Android ให้สิทธิ์เข้าถึงตำแหน่ง 2 อย่าง ได้แก่ ACCESS_COARSE_LOCATION และ ACCESS_FINE_LOCATION สิทธิ์ที่คุณเลือกจะเป็นตัวกำหนดความถูกต้องของ ตำแหน่งที่ API แสดงผล

  • android.permission.ACCESS_COARSE_LOCATION – อนุญาตให้ API ส่งคืนตำแหน่งโดยประมาณของอุปกรณ์ สิทธิ์ให้ค่าประมาณของตำแหน่งอุปกรณ์จากบริการตำแหน่ง เช่น ซึ่งอธิบายไว้ในเอกสารเกี่ยวกับตำแหน่งโดยประมาณ ความแม่นยำ
  • android.permission.ACCESS_FINE_LOCATION – ช่วยให้ API ระบุตำแหน่งที่แม่นยำที่สุดเท่าที่จะเป็นไปได้จาก ผู้ให้บริการตำแหน่งที่ให้บริการ รวมถึงระบบดาวเทียมนำร่อง (GPS) รวมถึง Wi-Fi และอินเทอร์เน็ตมือถือ

เพิ่มสิทธิ์ในไฟล์ Manifest ของแอป

หากจำเป็นต้องใช้ตำแหน่งโดยประมาณเพื่อให้แอปทำงานได้เท่านั้น ให้เพิ่ม สิทธิ์ ACCESS_COARSE_LOCATION ในไฟล์ Manifest ของแอป:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp" >
  ...
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  ...
</manifest>

อย่างไรก็ตาม หากต้องการใช้ตำแหน่งที่แน่นอน ให้เพิ่ม ACCESS_COARSE_LOCATION ทั้งคู่ และ ACCESS_FINE_LOCATION สิทธิ์ในไฟล์ Manifest ของแอป

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp" >
  ...
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  ...
</manifest>

ขอสิทธิ์รันไทม์

Android 6.0 (Marshmallow) แนะนำโมเดลใหม่สำหรับการจัดการสิทธิ์ซึ่ง ลดความซับซ้อนของกระบวนการสำหรับผู้ใช้เมื่อติดตั้งและอัปเกรดแอป หาก แอปกำหนดเป้าหมายเป็น API ระดับ 23 ขึ้นไป คุณสามารถใช้โมเดลสิทธิ์ใหม่ได้

หากแอปรองรับรูปแบบสิทธิ์ใหม่และอุปกรณ์ใช้ Android 6.0 (Marshmallow) ขึ้นไป ผู้ใช้ไม่ต้องให้สิทธิ์ใดๆ เมื่อลูกค้าติดตั้งหรืออัปเกรดแอป แอปต้องตรวจสอบว่า มีสิทธิ์ที่จำเป็นขณะรันไทม์ และจะขอสิทธิ์หากมี ถ้าไม่มี ระบบจะแสดงกล่องโต้ตอบที่ขอสิทธิ์จากผู้ใช้

คุณควรขอสิทธิ์ตามบริบทเพื่อให้ผู้ใช้ได้รับประสบการณ์ที่ดีที่สุด หากตำแหน่งมีความสำคัญต่อการทำงานของแอป คุณควรส่งคำขอ สิทธิ์เข้าถึงตำแหน่งเมื่อเริ่มต้นแอป วิธีที่ดีก็คือการอุ่นเครื่อง หน้าจอต้อนรับหรือวิซาร์ดที่จะให้ความรู้ผู้ใช้เกี่ยวกับเหตุผลที่ให้สิทธิ์นั้น ต้องระบุ

หากแอปต้องการสิทธิ์ฟังก์ชันการทำงานเพียงบางส่วน คุณจะต้อง ควรขอสิทธิ์เข้าถึงตำแหน่ง ณ เวลาที่แอปดำเนินการ การดำเนินการที่ต้องใช้สิทธิ์

แอปต้องจัดการกรณีที่ผู้ใช้ไม่ให้สิทธิ์อย่างเหมาะสม สิทธิ์ ตัวอย่างเช่น หากจำเป็นต้องใช้สิทธิ์สำหรับฟีเจอร์ใดฟีเจอร์หนึ่ง พารามิเตอร์ สามารถปิดใช้ฟีเจอร์นั้นได้ หากสิทธิ์นั้นจำเป็นสำหรับแอปในการ แอปจะปิดใช้ฟังก์ชันทั้งหมดของแอป แล้วแจ้งให้ผู้ใช้ทราบว่า ที่พวกเขาจำเป็นต้องให้สิทธิ์

ตัวอย่างโค้ดต่อไปนี้จะตรวจหาสิทธิ์ที่ใช้ไลบรารี AndroidX ก่อนที่จะเปิดใช้งานเลเยอร์ตำแหน่งของฉัน จากนั้นจะจัดการผลลัพธ์ของ คำขอสิทธิ์โดยใช้ ActivityCompat.OnRequestPermissionsResultCallback จากไลบรารีการสนับสนุน

Kotlin

// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.example.kotlindemos

import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.location.Location
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback
import androidx.core.content.ContextCompat
import com.example.kotlindemos.PermissionUtils.PermissionDeniedDialog.Companion.newInstance
import com.example.kotlindemos.PermissionUtils.isPermissionGranted
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener
import com.google.android.gms.maps.GoogleMap.OnMyLocationClickListener
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment

/**
 * This demo shows how GMS Location can be used to check for changes to the users location.  The
 * "My Location" button uses GMS Location to set the blue dot representing the users location.
 * Permission for [Manifest.permission.ACCESS_FINE_LOCATION] and [Manifest.permission.ACCESS_COARSE_LOCATION]
 * are requested at run time. If either permission is not granted, the Activity is finished with an error message.
 */
class MyLocationDemoActivity : AppCompatActivity(),
    OnMyLocationButtonClickListener,
    OnMyLocationClickListener, OnMapReadyCallback,
    OnRequestPermissionsResultCallback {
    /**
     * Flag indicating whether a requested permission has been denied after returning in
     * [.onRequestPermissionsResult].
     */
    private var permissionDenied = false
    private lateinit var map: GoogleMap
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.my_location_demo)
        val mapFragment =
            supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
        mapFragment?.getMapAsync(this)
    }

    override fun onMapReady(googleMap: GoogleMap) {
        map = googleMap
        googleMap.setOnMyLocationButtonClickListener(this)
        googleMap.setOnMyLocationClickListener(this)
        enableMyLocation()
    }

    /**
     * Enables the My Location layer if the fine location permission has been granted.
     */
    @SuppressLint("MissingPermission")
    private fun enableMyLocation() {

        // 1. Check if permissions are granted, if so, enable the my location layer
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) == PackageManager.PERMISSION_GRANTED
        ) {
            map.isMyLocationEnabled = true
            return
        }

        // 2. If if a permission rationale dialog should be shown
        if (ActivityCompat.shouldShowRequestPermissionRationale(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) || ActivityCompat.shouldShowRequestPermissionRationale(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            )
        ) {
            PermissionUtils.RationaleDialog.newInstance(
                LOCATION_PERMISSION_REQUEST_CODE, true
            ).show(supportFragmentManager, "dialog")
            return
        }

        // 3. Otherwise, request permission
        ActivityCompat.requestPermissions(
            this,
            arrayOf(
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ),
            LOCATION_PERMISSION_REQUEST_CODE
        )
    }

    override fun onMyLocationButtonClick(): Boolean {
        Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT)
            .show()
        // Return false so that we don't consume the event and the default behavior still occurs
        // (the camera animates to the user's current position).
        return false
    }

    override fun onMyLocationClick(location: Location) {
        Toast.makeText(this, "Current location:\n$location", Toast.LENGTH_LONG)
            .show()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
            super.onRequestPermissionsResult(
                requestCode,
                permissions,
                grantResults
            )
            return
        }

        if (isPermissionGranted(
                permissions,
                grantResults,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) || isPermissionGranted(
                permissions,
                grantResults,
                Manifest.permission.ACCESS_COARSE_LOCATION
            )
        ) {
            // Enable the my location layer if the permission has been granted.
            enableMyLocation()
        } else {
            // Permission was denied. Display an error message
            // Display the missing permission error dialog when the fragments resume.
            permissionDenied = true
        }
    }

    override fun onResumeFragments() {
        super.onResumeFragments()
        if (permissionDenied) {
            // Permission was not granted, display error dialog.
            showMissingPermissionError()
            permissionDenied = false
        }
    }

    /**
     * Displays a dialog with error message explaining that the location permission is missing.
     */
    private fun showMissingPermissionError() {
        newInstance(true).show(supportFragmentManager, "dialog")
    }

    companion object {
        /**
         * Request code for location permission request.
         *
         * @see .onRequestPermissionsResult
         */
        private const val LOCATION_PERMISSION_REQUEST_CODE = 1
    }
}

Java

// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


package com.example.mapdemo;

import android.Manifest.permission;
import android.annotation.SuppressLint;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener;
import com.google.android.gms.maps.GoogleMap.OnMyLocationClickListener;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.widget.Toast;

/**
 * This demo shows how GMS Location can be used to check for changes to the users location.  The "My
 * Location" button uses GMS Location to set the blue dot representing the users location.
 * Permission for {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and {@link
 * android.Manifest.permission#ACCESS_COARSE_LOCATION} are requested at run time. If either
 * permission is not granted, the Activity is finished with an error message.
 */
public class MyLocationDemoActivity extends AppCompatActivity
    implements
    OnMyLocationButtonClickListener,
    OnMyLocationClickListener,
    OnMapReadyCallback,
    ActivityCompat.OnRequestPermissionsResultCallback {

    /**
     * Request code for location permission request.
     *
     * @see #onRequestPermissionsResult(int, String[], int[])
     */
    private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;

    /**
     * Flag indicating whether a requested permission has been denied after returning in {@link
     * #onRequestPermissionsResult(int, String[], int[])}.
     */
    private boolean permissionDenied = false;

    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_location_demo);

        SupportMapFragment mapFragment =
            (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(@NonNull GoogleMap googleMap) {
        map = googleMap;
        map.setOnMyLocationButtonClickListener(this);
        map.setOnMyLocationClickListener(this);
        enableMyLocation();
    }

    /**
     * Enables the My Location layer if the fine location permission has been granted.
     */
    @SuppressLint("MissingPermission")
    private void enableMyLocation() {
        // 1. Check if permissions are granted, if so, enable the my location layer
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED
            || ContextCompat.checkSelfPermission(this, permission.ACCESS_COARSE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
            map.setMyLocationEnabled(true);
            return;
        }

        // 2. Otherwise, request location permissions from the user.
        PermissionUtils.requestLocationPermissions(this, LOCATION_PERMISSION_REQUEST_CODE, true);
    }

    @Override
    public boolean onMyLocationButtonClick() {
        Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
        // Return false so that we don't consume the event and the default behavior still occurs
        // (the camera animates to the user's current position).
        return false;
    }

    @Override
    public void onMyLocationClick(@NonNull Location location) {
        Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) {
        if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            return;
        }

        if (PermissionUtils.isPermissionGranted(permissions, grantResults,
            Manifest.permission.ACCESS_FINE_LOCATION) || PermissionUtils
            .isPermissionGranted(permissions, grantResults,
                Manifest.permission.ACCESS_COARSE_LOCATION)) {
            // Enable the my location layer if the permission has been granted.
            enableMyLocation();
        } else {
            // Permission was denied. Display an error message
            // Display the missing permission error dialog when the fragments resume.
            permissionDenied = true;
        }
    }

    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        if (permissionDenied) {
            // Permission was not granted, display error dialog.
            showMissingPermissionError();
            permissionDenied = false;
        }
    }

    /**
     * Displays a dialog with error message explaining that the location permission is missing.
     */
    private void showMissingPermissionError() {
        PermissionUtils.PermissionDeniedDialog
            .newInstance(true).show(getSupportFragmentManager(), "dialog");
    }

}

เลเยอร์ตำแหน่งของฉัน

คุณสามารถใช้เลเยอร์ตำแหน่งของฉันและปุ่มตำแหน่งของฉันเพื่อแสดง ตำแหน่งปัจจุบันของตนบนแผนที่ โทรติดต่อ mMap.setMyLocationEnabled() เพื่อเปิดใช้งานเลเยอร์ตำแหน่งของฉันบนแผนที่

ตัวอย่างต่อไปนี้แสดงการใช้งานแบบง่ายของเลเยอร์ตำแหน่งของฉัน

Kotlin



// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.maps.example.kotlin

import android.annotation.SuppressLint
import android.location.Location
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener
import com.google.android.gms.maps.GoogleMap.OnMyLocationClickListener
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.maps.example.R

internal class MyLocationLayerActivity : AppCompatActivity(),
    OnMyLocationButtonClickListener,
    OnMyLocationClickListener,
    OnMapReadyCallback {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my_location)
        val mapFragment =
            supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }

    @SuppressLint("MissingPermission")
    override fun onMapReady(map: GoogleMap) {
        // TODO: Before enabling the My Location layer, you must request
        // location permission from the user. This sample does not include
        // a request for location permission.
        map.isMyLocationEnabled = true
        map.setOnMyLocationButtonClickListener(this)
        map.setOnMyLocationClickListener(this)
    }

    override fun onMyLocationClick(location: Location) {
        Toast.makeText(this, "Current location:\n$location", Toast.LENGTH_LONG)
            .show()
    }

    override fun onMyLocationButtonClick(): Boolean {
        Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT)
            .show()
        // Return false so that we don't consume the event and the default behavior still occurs
        // (the camera animates to the user's current position).
        return false
    }
}


      

Java


// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.maps.example;

import android.annotation.SuppressLint;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

class MyLocationLayerActivity extends AppCompatActivity
    implements GoogleMap.OnMyLocationButtonClickListener,
    GoogleMap.OnMyLocationClickListener,
    OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_location);

        SupportMapFragment mapFragment =
            (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onMapReady(GoogleMap map) {
        // TODO: Before enabling the My Location layer, you must request
        // location permission from the user. This sample does not include
        // a request for location permission.
        map.setMyLocationEnabled(true);
        map.setOnMyLocationButtonClickListener(this);
        map.setOnMyLocationClickListener(this);
    }

    @Override
    public void onMyLocationClick(@NonNull Location location) {
        Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG)
            .show();
    }

    @Override
    public boolean onMyLocationButtonClick() {
        Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT)
            .show();
        // Return false so that we don't consume the event and the default behavior still occurs
        // (the camera animates to the user's current position).
        return false;
    }
}


      

เมื่อเปิดใช้งานเลเยอร์ตำแหน่งของฉัน ปุ่มตำแหน่งของฉันจะปรากฏขึ้นใน มุมขวาบนของแผนที่ เมื่อผู้ใช้คลิกปุ่ม กล้องจะอยู่ตรงกลาง แผนที่บนตำแหน่งปัจจุบันของอุปกรณ์ หากทราบ สถานที่ จะมีจุดสีน้ำเงินเล็กๆ แสดงอยู่บนแผนที่หากอุปกรณ์อยู่นิ่งๆ หรือเป็นเครื่องหมายบั้ง ถ้าอุปกรณ์มีการเคลื่อนไหว

ภาพหน้าจอต่อไปนี้แสดงปุ่มตำแหน่งของฉันที่ด้านขวาบนและ จุดสีน้ำเงินตำแหน่งของฉันตรงกลางแผนที่:

คุณสามารถป้องกันไม่ให้ปุ่มตำแหน่งของฉันปรากฏขึ้นโดยการโทร UiSettings.setMyLocationButtonEnabled(false)

แอปของคุณสามารถตอบสนองต่อเหตุการณ์ต่อไปนี้

  • หากผู้ใช้คลิกปุ่ม "ตำแหน่งของฉัน" แอปของคุณจะได้รับ onMyLocationButtonClick() Callback จาก GoogleMap.OnMyLocationButtonClickListener
  • หากผู้ใช้คลิกที่จุดสีน้ำเงิน "ตำแหน่งของฉัน" แอปของคุณจะได้รับ onMyLocationClick() Callback จาก GoogleMap.OnMyLocationClickListener

API ตำแหน่งของบริการ Google Play

แนะนำให้ใช้ Location API ของบริการ Google Play สำหรับการเพิ่มการรับรู้ตำแหน่งลงในแอปพลิเคชัน Android ของคุณ ซึ่งรวมถึง ฟังก์ชันที่ทำให้คุณสามารถ

  • ระบุตำแหน่งของอุปกรณ์
  • ฟังการเปลี่ยนแปลงสถานที่
  • ระบุรูปแบบการเดินทาง หากอุปกรณ์เคลื่อนที่
  • สร้างและตรวจสอบพื้นที่ทางภูมิศาสตร์ที่กำหนดไว้ล่วงหน้า ซึ่งเรียกว่าเขตพื้นที่เสมือน

API ตำแหน่งจะช่วยให้คุณสร้าง การประหยัดพลังงานได้ง่ายๆ แอปพลิเคชันที่รับรู้ตำแหน่ง เช่นเดียวกับแผนที่ SDK สำหรับ Android ตำแหน่ง API ได้รับการเผยแพร่เป็นส่วนหนึ่งของ SDK บริการ Google Play สำหรับข้อมูลเพิ่มเติม ข้อมูลเกี่ยวกับ API สถานที่ โปรดดูคลาสการฝึกอบรมสำหรับ Android การกำหนดให้แอปรับรู้ตำแหน่งหรือ Location API ข้อมูลอ้างอิง ตัวอย่างโค้ดมีรวมอยู่ด้วยโดยเป็นส่วนหนึ่งของบริการ SDK บริการ Google Play