Giới thiệu
Bài viết này hướng dẫn cách lấy vị trí GPS hiện tại của thiết bị Android thông qua code lập trình, sử dụng FusedLocationProviderClient từ Google Play Services – cách tiếp cận hiện đại và tiết kiệm pin hơn so với LocationManager truyền thống.
Các bước thực hiện
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project, điền thông tin cần thiết và tạo dự án.
Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vị trí GPS hiện tại"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="@+id/getLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lấy vị trí" />
</LinearLayout>
Bước 3: Viết code logic (src/MainActivity.java)
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
public class MainActivity extends AppCompatActivity {
private FusedLocationProviderClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
client = LocationServices.getFusedLocationProviderClient(this);
Button button = findViewById(R.id.getLocation);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
client.getLastLocation().addOnSuccessListener(MainActivity.this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
TextView textView = findViewById(R.id.location);
textView.setText("Vĩ độ: " + location.getLatitude() + "\nKinh độ: " + location.getLongitude());
}
}
});
}
});
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, 1);
}
}
Bước 4: Cấu hình quyền trong AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chạy ứng dụng
Kết nối thiết bị Android thật với máy tính, nhấn Run trong Android Studio, chọn thiết bị và kiểm tra kết quả. Ứng dụng sẽ hiển thị vĩ độ và kinh độ khi nhấn nút "Lấy vị trí".
Lưu ý quan trọng
- Cần cấp quyền
ACCESS_FINE_LOCATIONtại runtime (Android 6.0+) - Thiết bị phải bật GPS và có kết nối internet để định vị chính xác
- FusedLocationProviderClient tự động chọn nguồn định vị tối ưu (GPS, Wi-Fi, mạng di động)
- Phương thức
getLastLocation()trả về vị trí cuối cùng đã biết, có thể null nếu chưa từng định vị