كيف يمكنني العثور على خط العرض وخط الطول من العنوان؟

StackOverflow https://stackoverflow.com/questions/3574644

سؤال

أريد أن أعرض موقع عنوان في خرائط Google.

كيف أحصل على خط العرض وخط الطول للعنوان باستخدام واجهة برمجة تطبيقات خرائط Google؟

هل كانت مفيدة؟

المحلول

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 متغير يحمل العناوين المحولة.

نصائح أخرى

حل UD_AN مع API المحدث

ملحوظة: latlng الفصل هو جزء من Google Play Services.

إلزامي:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<uses-permission android:name="android.permission.INTERNET"/>

تحديث: إذا كان لديك مستهدف SDK 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;
}

إذا كنت ترغب في وضع عنوانك في خريطة Google ، فعندئذٍ سهلة الاستخدام المتابعة

Intent searchAddress = new  Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address));
startActivity(searchAddress);

أو

إذا كنت بحاجة إلى الحصول على LAT لفترة طويلة من عنوانك ثم استخدم Google Place API التالية

قم بإنشاء طريقة تُرجع أ jsonobject مع استجابة مكالمة HTTP مثل ما يلي

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 إلى طريقة getlatlong () مثل المتابعة

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;
    }

آمل أن يساعدك هذا في الآخرين .. !! شكرًا لك..!!

سوف يعمل الرمز التالي لـ Google APIV2:

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 Testing Rd City State Zip) تريد تحويلها إلى Latlng.

هذه هي الطريقة التي يمكنك بها العثور على خطوط الطول وعرض خط الطول حيث ننقر على الخريطة.

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 غير متوفرة" لقد أعطيت بالفعل هذه الإذن وأدرجت المكتبة ... يمكنني الحصول على عرض الخريطة ... يلقي أن ioException في Geocoder ...

لقد أضفت للتو catch 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();
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top