从Android中的地址获取经度和纬度

时间:2014-03-13 13:07:47

标签: android openstreetmap osmdroid

现在,当我从当前位置获取地址时,现在我将从地址中获取经度和纬度

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

我用这个来获取地址而我不知道如何从地址获得纬度和经度?

4 个答案:

答案 0 :(得分:1)

试试这段代码:

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

答案 1 :(得分:0)

这是我在其中一个应用中使用过的代码段。

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

从如此获得的地址列表中,您可以使用获取所需的字段。

答案 2 :(得分: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();
}

答案 3 :(得分:0)

尝试this链接&amp;使用this链接 Google地理编码API &amp;查看 David Kristian Laundav 发布的答案,以获取从Android地址获取纬度,经度

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

返回此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"
 }

解析这个Json。 希望这会有所帮助。

相关问题