Giới thiệu
Bài viết này hướng dẫn cách tính khoảng cách giữa hai tọa độ địa lý (latitude, longitude) sử dụng class android.location.Location có sẵn trong Android SDK.
Các bước thực hiện
Bước 1: Tạo dự án mới
Khởi tạo project Android Studio mới.
Bước 2: Layout đơn giản (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
Bước 3: Code Java tính khoảng cách (src/MainActivity.java)
package app.com.sample;
import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
// Tọa độ vị trí A (Ví dụ: Bến Thành, TP.HCM)
Location locationA = new Location("locationA");
locationA.setLatitude(10.771975);
locationA.setLongitude(106.698145);
// Tọa độ vị trí B (Ví dụ: Nhà hát lớn TP.HCM)
Location locationB = new Location("locationB");
locationB.setLatitude(10.779556);
locationB.setLongitude(106.700927);
// Tính khoảng cách (kết quả tính bằng MET)
float distanceInMeters = locationA.distanceTo(locationB);
// Chuyển đổi sang km
double distanceInKm = distanceInMeters / 1000.0;
textView.setText(String.format(
"Khoảng cách giữa 2 vị trí: %.2f mét (%.3f km)",
distanceInMeters, distanceInKm
));
}
}
Giải thích:
Location.distanceTo(Location): Trả về khoảng cách tính bằng mét- Sử dụng công thức Haversine nội bộ (đính chính độ cong Trái Đất)
- Không cần quyền GPS/Internet - chỉ tính toán toán học
Bước 4: Manifest
<?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=".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
Chạy app, TextView sẽ hiển thị khoảng cách giữa 2 điểm đã định nghĩa sẵn.

Mở rộng: Tính khoảng cách thực tế theo đường đi
Nếu cần khoảng cách theo đường đường bộ (không phải đường chim bay), sử dụng Google Directions API hoặc Mapbox Navigation SDK.
Kotlin Extension Function
fun Location.distanceToKm(other: Location): Double {
return distanceTo(other) / 1000.0
}
// Sử dụng:
val distanceKm = locationA.distanceToKm(locationB)