【问题标题】:Get latitude,longitude from address in Android从Android中的地址获取纬度,经度
【发布时间】:2013-07-24 00:18:00
【问题描述】:

我想从地址中获取某个地方的纬度和经度。 我不想从 GPS 或网络中获取纬度和经度。

我希望用户能够在 TextView 中输入他想要的地点(例如他的办公室)的地址,并且在文本字段下方会显示一些建议,就像您在谷歌地图应用程序中输入地址时一样.然后我想检索给定地址的坐标。

如果无法做到这一点,有没有办法通过谷歌地图应用程序本身获取地址。也许我可以从我的应用程序中调用 gmaps,用户将输入地址,gmaps 将返回坐标。

我该怎么做?

【问题讨论】:

    标签: android location coordinates


    【解决方案1】:

    Android 框架中提供的 Geocoder API

    我以前使用过Geocoding API which exists in the Android API,但它不适用于所有设备。事实上,根据我和其他人的经验,很多设备在使用 Geocoding API 时会返回 null。

    出于这个原因,我选择使用反向地理编码器,它可以在所有设备上完美运行,但由于 HTTP 请求需要额外的开销。

    使用反向地理编码 API 从地址中检索经度和纬度

    为避免此问题,只需使用返回 JSON 对象的 Reverse Geocoding API 即可。

    您可以使用API​​通过经纬度查找地址,也可以通过地址查找经纬度:

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

    返回:

    {
       "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"
    }
    

    在此,使用用户输入的地址向反向地理编码 API 发送 HTTP 请求,然后解析 JSON 对象以查找您需要的数据是一件简单的事情。

    之前的一篇文章描述了a JSON object can be parsed from an URL.

    希望这会有所帮助。

    【讨论】:

    • 我花了 2 多个小时试图让它与地理编码 API 一起工作,但没有运气。 Geocoder.getFromLocationName(address, x) 始终返回 null。这条路就是要走的路。感谢您的回答!
    • 我在多个设备上遇到了完全相同的问题;它对某些人有效,但到目前为止还不是全部。这种方法几乎可以保证工作,但与地理编码器相比需要开销。但我个人会在任何一天将可靠性切换到性能:) 很高兴它有所帮助。
    【解决方案2】:

    您可以使用以下方法:

            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】:
      /**
       * @method getLocationFromAddress
       * @param strAddress Address/Location String
       * @desc Get searched location points from address and plot/update on map.
       */
      public void getLocationFromAddress(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) {
                  return;
              }
      
              //Lets take first possibility from the all possibilities.
              Address location=address.get(0);
              LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
      
              //Put marker on map on that LatLng
              Marker srchMarker = mMap.addMarker(new MarkerOptions().position(latLng).title("Destination").icon(BitmapDescriptorFactory.fromResource(R.drawable.bb)));
      
              //Animate and Zoon on that map location
              mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
              mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
      
          } catch (IOException e)
          {
              e.printStackTrace();
          }
      }
      

      【讨论】:

        【解决方案4】:

        您需要的是:http://developer.android.com/reference/android/location/Geocoder.html

        你可以这样使用它:

        List<Address> result = new Geocoder(context).getFromLocationName(locationName, maxResults);
        

        您将获得Address 的列表,然后您可以在这些地址上调用getLatitudegetLongitude

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-12-02
          • 1970-01-01
          • 2013-07-04
          • 1970-01-01
          • 2012-02-15
          • 1970-01-01
          相关资源
          最近更新 更多