উত্তর:
public GeoPoint getLocationFromAddress(String strAddress){
Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;
try {
address = coder.getFromLocationName(strAddress,5);
if (address==null) {
return null;
}
Address location=address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((double) (location.getLatitude() * 1E6),
(double) (location.getLongitude() * 1E6));
return p1;
}
}
strAddress
ঠিকানা যুক্ত একটি স্ট্রিং। address
পরিবর্তনশীল রূপান্তরিত ঠিকানাগুলি ঝুলিতে।
GeoPoint
নতুন লাইব্রেরিতে কোনও শ্রেণি আছে। পরিবর্তে ব্যবহার LatLng
। stackoverflow.com/a/27834110/2968401
আপডেট হওয়া এপিআই এর সাথে উদ_র সমাধান
দ্রষ্টব্য : ল্যাটল্যাং ক্লাসটি গুগল প্লে পরিষেবাদির অংশ।
বাধ্যতামূলক :
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
আপডেট: আপনার যদি 23 এবং তদূর্ধ্বের লক্ষ্যবস্তু রয়েছে, তবে নিশ্চিত হয়ে নিন যে আপনি অবস্থানের জন্য রানটাইম অনুমতিের যত্ন নিচ্ছেন।
public LatLng getLocationFromAddress(Context context,String strAddress) {
Geocoder coder = new Geocoder(context);
List<Address> address;
LatLng p1 = null;
try {
// May throw an IOException
address = coder.getFromLocationName(strAddress, 5);
if (address == null) {
return null;
}
Address location = address.get(0);
p1 = new LatLng(location.getLatitude(), location.getLongitude() );
} catch (IOException ex) {
ex.printStackTrace();
}
return p1;
}
if(address.size() <1){//show a Toast}else{//put rest of code here}
আপনি যদি নিজের ঠিকানা গুগল ম্যাপে রাখতে চান তবে নীচের ব্যবহারের সহজ উপায়
Intent searchAddress = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address));
startActivity(searchAddress);
অথবা
আপনার যদি নিজের ঠিকানা থেকে দীর্ঘতর প্রয়োজন হয় তবে নিম্নলিখিত স্থানে গুগল প্লেস এপিআই ব্যবহার করুন
নীচের মতো HTTP কলের প্রতিক্রিয়া সহ একটি JSONObject প্রদান করে এমন একটি পদ্ধতি তৈরি করুন
public static JSONObject getLocationInfo(String address) {
StringBuilder stringBuilder = new StringBuilder();
try {
address = address.replaceAll(" ","%20");
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
ল্যাটলং () পদ্ধতিটি নীচের মতো পাওয়ার জন্য এখন সেই JSONObject পাস করুন
public static boolean getLatLong(JSONObject jsonObject) {
try {
longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
} catch (JSONException e) {
return false;
}
return true;
}
আমি আশা করি এটি আপনাকে অন্যকে সহায়তা করবে .. !! ধন্যবাদ..!!
নিম্নলিখিত কোডটি গুগল এপিভ 2 এর জন্য কাজ করবে:
public void convertAddress() {
if (address != null && !address.isEmpty()) {
try {
List<Address> addressList = geoCoder.getFromLocationName(address, 1);
if (addressList != null && addressList.size() > 0) {
double lat = addressList.get(0).getLatitude();
double lng = addressList.get(0).getLongitude();
}
} catch (Exception e) {
e.printStackTrace();
} // end catch
} // end if
} // end convertAddress
যেখানে ঠিকানাটি স্ট্রিং (123 টেস্টিং আরডি সিটি স্টেট জিপ) আপনি ল্যাটলংতে রূপান্তর করতে চান।
আমরা মানচিত্রে যেখানে ক্লিক করেছি তার দ্রাঘিমাংশ এবং দ্রাঘিমাংশকে এভাবেই আপনি আবিষ্কার করতে পারেন।
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1)
{
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
}
return false;
}
এটা ভাল কাজ করে.
অবস্থানের ঠিকানা পেতে আমরা জিওকোডার ক্লাস ব্যবহার করতে পারি।
উপরের কাঁধ সমস্যার উত্তর:
এটি "java.io.IOException পরিষেবাটি উপলভ্য নয়" ছুড়ে ফেলেছে আমি ইতিমধ্যে সেই অনুমতি দিয়েছি এবং লাইব্রেরিটি অন্তর্ভুক্ত করেছি ... আমি মানচিত্রের দৃশ্য পেতে পারি ... এটি জিওকোডারটিতে আইওএক্সেপশন নিক্ষেপ করে ...
আমি চেষ্টা করার পরে কেবল একটি ক্যাচ আইওএক্সেপশন যুক্ত করেছি এবং এটি সমস্যার সমাধান করেছে
catch(IOException ioEx){
return null;
}
Geocoder coder = new Geocoder(this);
List<Address> addresses;
try {
addresses = coder.getFromLocationName(address, 5);
if (addresses == null) {
}
Address location = addresses.get(0);
double lat = location.getLatitude();
double lng = location.getLongitude();
Log.i("Lat",""+lat);
Log.i("Lng",""+lng);
LatLng latLng = new LatLng(lat,lng);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
googleMap.addMarker(markerOptions);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,12));
} catch (IOException e) {
e.printStackTrace();
}
public void goToLocationFromAddress(String strAddress) {
//Create coder with Activity context - this
Geocoder coder = new Geocoder(this);
List<Address> address;
try {
//Get latLng from String
address = coder.getFromLocationName(strAddress, 5);
//check for null
if (address != null) {
//Lets take first possibility from the all possibilities.
try {
Address location = address.get(0);
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
//Animate and Zoon on that map location
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
} catch (IndexOutOfBoundsException er) {
Toast.makeText(this, "Location isn't available", Toast.LENGTH_SHORT).show();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}