【问题标题】:Get the latitude and longitude from Address in Android从Android中的地址获取经纬度
【发布时间】:2014-03-13 13:07:46
【问题描述】:

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

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

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

【问题讨论】:

  • 只是代码中的一个小错误,正确的方法见我的answer
  • 我没有收到你的问题,要么你想要来自 Lat/Long 的地址,要么你想要来自地址的 Lat/Long?
  • @inef 这里也一样。所以你从lat/long获取地址,然后想要地址的lat/long?
  • @Kedarnath 谢谢你我尝试了我的代码来检查这个是否正确,直到现在我没有问题
  • @i.n.e.f 我想要地址的纬度/经度,我可以将 makrer 导航到这一点:/我的英语不太好

标签: android openstreetmap osmdroid


【解决方案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;
}

【讨论】:

  • 什么是 JSON?这边
  • json 只是一种将数据保存在单个对象中的方法,我们将其作为结果传递,您可以根据需要使用另一个对象。
  • 有了这个我会从每个地址得到纬度/经度?
  • 您必须将 lat\long 作为参数传递才能从该 lat\long 获取地址。
【解决方案2】:

这是我在我的一个应用中使用的代码 sn-p。

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

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

【讨论】:

  • 我想我问错了..使用您的代码,我将获得地址和位置信息,例如我在哪个区域,但我必须知道如何从地址获取纬度/经度跨度>
【解决方案3】:
   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();
}

【讨论】:

  • 有了这个我会从每个地址得到纬度/经度?我有edittext,我可以在其中提供地址,现在我需要正确的方法来获取字符串并给我纬度/经度...我搜索我认为反向地理编码是正确的,但我不知道如何制作这个跨度>
  • 当我把它放在 Arraylist
    中我可以得到 lat/long withu ur code 对吗?
  • ejjectly,double lat 和 double long 包含你的 latlong
  • 但我无法将编辑文本添加到数组列表地址,也无法将其从编辑地址转换为地址
  • String s =youredittext.gettext().tostring();地址.add(s);
【解决方案4】:

尝试this 链接并使用this 链接Google 地理编码API 并查看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。 希望这会有所帮助。

【讨论】:

  • 问题是关于 OpenStreetMap,而不是 Google。
猜你喜欢
  • 2013-07-24
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
相关资源
最近更新 更多