1

Now as I get the Address from my current Position and now I will get the latitude and longitude from the Address

   Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
        List<Address> addresses;
        try {
            addresses = gcd.getFromLocation(location.getLatitude(),
                    location.getLongitude(), 1);
            if (addresses.size() > 0)
                java.lang.System.out.println(addresses.get(0).getLocality());
            cityName = addresses.get(0).getAddressLine(0);
            plz = addresses.get(0).getAddressLine(1);
            Land = addresses.get(0).getAddressLine(2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String s = longitude + "\n" + latitude + "\n\n"
            + cityName+ "\n" + plz + "\n"+ Land;
//          String l = "\n\nMy Current Land is: " + Land;
        txt.setText(s);

I used this one to get the Address and I don't know how to get from an Address the latitude and the longitude?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Biri
  • 48
  • 7
  • Just a little mistake in your code, See my [answer](http://stackoverflow.com/a/21930761/3330969) for proper way. – Lucifer Mar 13 '14 at 13:10
  • i am not getting your question Either you want Address from Lat/Long OR you want Lat/Long From Address? – i.n.e.f Mar 13 '14 at 13:18
  • @inef Same here. So you obtain the address from lat/long, and then want the lat/long of the address? – super-qua Mar 13 '14 at 13:22
  • @Kedarnath Thank u I tried my code out side to check if this one is right till now i had no Problem – Biri Mar 13 '14 at 13:31
  • @i.n.e.f I want for the Addresses the lat/long that i can navigate the makrer to this point :/ my english is not so well – Biri Mar 13 '14 at 13:33
  • @i.n.e.f I want Lat/Long From Address :) – Biri Mar 13 '14 at 13:46

4 Answers4

1

try this code :

public static String getLocationData(Context mContext, double lat,
        double lng) {
    String address = "";
    String county = "";
    String area = "";
    String city = "";
    String state = "";
    String countryCode = "";
    String postalCode = "";
    String add = "";

    // JSONArray jsonArray = new JSONArray();
    JSONObject json = new JSONObject();

    Geocoder gCoder = new Geocoder(mContext);
    ArrayList<Address> addresses = null;
    try {
        addresses = (ArrayList<Address>) gCoder
                .getFromLocation(lat, lng, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (addresses != null && addresses.size() > 0) {

        Address addressLocation = addresses.get(0);

        for (int i = 0; i <= addressLocation.getMaxAddressLineIndex(); i++) {
            String coma = (i == 0) ? "" : ", ";

            add += coma + addressLocation.getAddressLine(i);
        }
        county = addressLocation.getCountryName();
        area = addressLocation.getSubLocality();
        city = addressLocation.getLocality();
        state = addressLocation.getAdminArea();
        countryCode = addressLocation.getCountryCode();
        postalCode = addressLocation.getPostalCode();
    }

    TimeZone tz = TimeZone.getDefault();
    Date date = new Date();

    try {
        json.put("latitude", lat);
        json.put("longitude", lng);
        json.put("address", (add == null) ? "" : add);
        json.put("area", (area == null) ? "" : area);
        json.put("city", (city == null) ? "" : city);
        json.put("state", (state == null) ? "" : state);
        json.put("contry_name", (county == null) ? "" : county);
        json.put("contry_code", (countryCode == null) ? "" : countryCode);
        json.put("postal_code", (postalCode == null) ? "" : postalCode);

        String id = tz.getID();
        json.put("time_zone", tz.getID());
        json.put("time_zone_offset", tz.getOffset(date.getTime()) / 1000);
        json.put("time_zone_label", tz.getDisplayName());
    } catch (Exception e) {
        e.printStackTrace();
    }
    address = json.toString();
    return address;
}
Vijju
  • 3,458
  • 1
  • 22
  • 20
0

This is a code snippet I have used in one of my apps.

public void getLocationName(Context context,double latitude,double longitude,String callBackMethodName)
    {
        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (callBackMethodName == null || callBackMethodName.equals(""))
                return;
            try 
            {
                Method callBackMethod =context.getClass().getDeclaredMethod(callBackMethodName,List.class);
                callBackMethod.invoke(context,addresses);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

From the addresses list so obtained you can use to get your required field.

Bette Devine
  • 1,196
  • 1
  • 9
  • 23
  • I think I asked wrong..with ur code i will get the address and location info like in which area i am but i have to know how i will get the lat/long from an Address – Biri Mar 13 '14 at 13:38
0
   Geocoder coder = new Geocoder(this);
try {
    ArrayList<Address> adresses = (ArrayList<Address>) coder.getFromLocationName("Your Address", 50);
    for(Address add : adresses){
        if (statement) {//Controls to ensure it is right address such as country etc.
            double longitude = add.getLongitude();
            double latitude = add.getLatitude();
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
  • with this one i will get the lat/long from every address ? i have edittext where i can give the address and now i need the right method that it take the strings and give me that the lat/long...i searched i think reverse geocoding is right but i dont no how to make this – Biri Mar 13 '14 at 13:44
  • and when I put this in the Arraylist
    I can get the lat/long withu ur code right ?
    – Biri Mar 13 '14 at 13:49
  • ejjectly,double lat and double long contains your latlong – Pankaj Arora Mar 13 '14 at 13:51
  • but i cant add edit text to the arraylist address and i cant cast it from edit to address – Biri Mar 13 '14 at 13:59
  • String s =youredittext.gettext().tostring(); adresses.add(s); – Pankaj Arora Mar 13 '14 at 14:00
  • thank u very much that u take the time to help..but i have a last question when i give the postalcode in the edittext it will be work for searching the lat/long ? – Biri Mar 13 '14 at 14:06
  • --The method add(Address) in the type ArrayList
    is not applicable for the arguments (String)-- this happens when i add it to the list
    – Biri Mar 13 '14 at 14:46
0

try this link & use this Link For The Google Geocoding API & check the Answer posted By David Kristian Laundav to get the Get latitude,longitude from address in Android.

call http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false

Which return this Json

     {
       "results" : [
      {
     "address_components" : [
        {
           "long_name" : "1600",
           "short_name" : "1600",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Amphitheatre Pkwy",
           "short_name" : "Amphitheatre Pkwy",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Mountain View",
           "short_name" : "Mountain View",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Santa Clara",
           "short_name" : "Santa Clara",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "California",
           "short_name" : "CA",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "94043",
           "short_name" : "94043",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
     "geometry" : {
        "location" : {
           "lat" : 37.42291810,
           "lng" : -122.08542120
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 37.42426708029149,
              "lng" : -122.0840722197085
           },
           "southwest" : {
              "lat" : 37.42156911970850,
              "lng" : -122.0867701802915
           }
        }
     },
     "types" : [ "street_address" ]
  }
  ],
   "status" : "OK"
 }

And Parse this Json. Hope this helps.

Community
  • 1
  • 1
i.n.e.f
  • 1,773
  • 13
  • 22