एंड्रॉइड में Geocoder का उपयोग करके एड्रेस से लैटीट्यूड/लॉन्गिट्यूड प्राप्त करना
Geocoding = एड्रेस (स्ट्रिंग) → लैटीट्यूड/लॉन्गिट्यूड (निर्देशांक)। एंड्रॉइड में android.location.Geocoder क्लास यह काम करती है। इसके लिए **इंटरनेट** और **लोकेशन परमिशन** चाहिए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio → File → New → New Project → "Empty Activity" → Finish।
चरण 2: Permissions (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.geocodingdemo">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- ACCESS_COARSE_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/Theme.GeocodingDemo">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
चरण 3: 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:orientation="vertical"
android:padding="24dp"
android:gravity="center">
<EditText
android:id="@+id/etAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="एड्रेस दर्ज करें (उदाहरण: इंडिया गेट, नई दिल्ली)"
android:inputType="text"
android:layout_marginBottom="16dp" />
<Button
android:id="@+id/btnGeocode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="निर्देशांक प्राप्त करें"
android:layout_marginBottom="16dp" />
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:lineSpacingExtra="4dp"
android:text="परिणाम यहां दिखेगा..." />
</LinearLayout>
चरण 4: MainActivity.java — Geocoder उपयोग (बैकग्राउंड थ्रेड में)
⚠️Geocoder.getFromLocationName()**नेटवर्क कॉल** करता है — **मेन थ्रेड पर न चलाएं** (ANR होगा)। उदाहरण मेंThread+Handlerउपयोग किया गया है; आधुनिक कोड में Coroutines/RxJava बेहतर हैं।
package com.example.geocodingdemo;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private EditText etAddress;
private Button btnGeocode;
private TextView tvResult;
// Handler मेन थ्रेड पर UI अपडेट के लिए
private final Handler handler = new Handler(Looper.getMainLooper(), msg -> {
if (msg.what == 1) {
Bundle bundle = msg.getData();
String result = bundle.getString("address", "त्रुटि: कोई डेटा नहीं");
tvResult.setText(result);
}
return true;
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etAddress = findViewById(R.id.etAddress);
btnGeocode = findViewById(R.id.btnGeocode);
tvResult = findViewById(R.id.tvResult);
btnGeocode.setOnClickListener(v -> {
String address = etAddress.getText().toString().trim();
if (address.isEmpty()) {
tvResult.setText("⚠️ कृपया एड्रेस दर्ज करें");
return;
}
tvResult.setText("🔍 खोज जारी है...");
geocodeAddress(address);
});
}
private void geocodeAddress(String address) {
new Thread(() -> {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
String result;
try {
// maxResults = 1 (पहला मैच)
List<Address> addresses = geocoder.getFromLocationName(address, 1);
if (addresses != null && !addresses.isEmpty()) {
Address location = addresses.get(0);
double lat = location.getLatitude();
double lng = location.getLongitude();
result = String.format(Locale.getDefault(),
"एड्रेस: %s\n\nअक्षांश (Latitude): %.6f\nदेशांतर (Longitude): %.6f",
address, lat, lng);
} else {
result = "❌ इस एड्रेस के लिए कोई परिणाम नहीं मिला";
}
} catch (IOException e) {
e.printStackTrace();
result = "❌ नेटवर्क त्रुटि: " + e.getMessage();
}
// Handler को रिजल्ट भेजें
Message msg = handler.obtainMessage(1);
Bundle bundle = new Bundle();
bundle.putString("address", result);
msg.setData(bundle);
msg.sendToTarget();
}).start();
}
}
चरण 5: चलाएं और टेस्ट करें
- ऐप खोलें।
- एड्रेस टाइप करें:
इंडिया गेट, नई दिल्लीयाTaj Mahal, Agraया1600 Amphitheatre Parkway, Mountain View, CA - "निर्देशांक प्राप्त करें" दबाएं।
- कुछ सेकंड में लैटीट्यूड/लॉन्गिट्यूड दिखेंगे।
🔄 रिवर्स जियोकोडिंग (निर्देशांक → एड्रेस)
// लैटीट्यूड/लॉन्गिट्यूड से एड्रेस प्राप्त करें
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (!addresses.isEmpty()) {
Address addr = addresses.get(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= addr.getMaxAddressLineIndex(); i++) {
sb.append(addr.getAddressLine(i)).append("\n");
}
// sb.toString() में पूरा फॉर्मैटेड एड्रेस
}
⚠️ सामान्य समस्याएं और समाधान
| समस्या | कारण | समाधान |
|---|---|---|
| खाली लिस्ट लौटती है | नेटवर्क नहीं, गलत एड्रेस, सेवा अनुपलब्ध | इंटरनेट चेक करें, एड्रेस सरल करें, रीट्राई लॉजिक जोड़ें |
| IOException: "Service not available" | डिवाइस पर Geocoder बैकएंड नहीं (कुछ एमुलेटर्स) | वास्तविक डिवाइस पर टेस्ट करें, या Google Maps Geocoding API (REST) उपयोग करें |
| बहुत धीमा | नेटवर्क लेटेंसी | कैशिंग, डिबाउंसिंग, बैकग्राउंड थ्रेड |
| परिणाम गलत/अस्पष्ट | एड्रेस अस्पष्ट है | अधिक विशिष्ट एड्रेस दें (पिनकोड, शहर, देश जोड़ें) |
🌐 विकल्प: Google Maps Geocoding API (REST, अधिक विश्वसनीय)
अगर डिवाइस Geocoder भरोसेमंद नहीं है (खासकर एमुलेटर/चीन डिवाइस), सर्वर-साइड या क्लाइंट-साइड REST API उपयोग करें:
// Retrofit + Gson उदाहरण
interface GeocodingApi {
@GET("maps/api/geocode/json")
fun geocode(@Query("address") String address,
@Query("key") String apiKey): Call<GeocodeResponse>
}
// बिल्ड.ग्रेडल
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
इसके लिए Google Cloud Console → APIs → Geocoding API इनेबल करें → API Key जनरेट करें।
सारांश
- ✅
Geocoder.getFromLocationName()एड्रेस → निर्देशांक के लिए। - ✅
Geocoder.getFromLocation()निर्देशांक → एड्रेस के लिए (रिवर्स जियोकोडिंग)। - ⚠️ **हमेशा बैकग्राउंड थ्रेड में चलाएं** — नेटवर्क कॉल करता है।
- ⚠️ **इंटरनेट + ACCESS_FINE_LOCATION परमिशन** जरूरी।
- 🔄 उत्पादन ऐप्स के लिए **Google Maps Geocoding API (REST)** अधिक विश्वसनीय और फीचर-रिच है।