在安卓中获取地址的纬度和经度

11

我正在尝试从地址中获取纬度和经度,问题在于...当我只提供城市名称时,它会给出正确的纬度和经度,但是当我提供完整的地址(例如州名、城市名称、街道号码)时,则不会给出正确的纬度和经度...

感谢您的合作回应...

我的代码如下:

//String addressStr = "faisalabad";/// this give me correct address
        String addressStr = "Sainta Augustine,FL,4405 Avenue A";
          Geocoder geoCoder = new Geocoder(MapClass.this, Locale.getDefault());

          try {
              List<Address> addresses =
          geoCoder.getFromLocationName(addressStr, 1); 
              if (addresses.size() >  0) {
                 latitude = addresses.get(0).getLatitude(); longtitude =
          addresses.get(0).getLongitude(); }

          } catch (IOException e) { // TODO Auto-generated catch block
          e.printStackTrace(); }


        pos = new LatLng(latitude, longtitude);
        googleMap.addMarker(new MarkerOptions().icon(
                BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED))
                .position(pos));
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(pos, 15));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

它是否抛出了一些错误? - Divya Motiwala
没有错误, 默认的纬度和经度值为0.0,在海洋中放置标记... - mohsin raza
你的getDefault()使用的是哪个语言环境? - Stefan de Bruijn
什么都没有,这是默认值。 - mohsin raza
你有使用不同的地理编码器的选项吗? - Jeffrey
2个回答

6
我翻译成中文为:

我明白了... :)

只是顺序不对...

先写街道地址,然后写城市名,最后写州名... 这样会从地址中给我正确的经纬度.. :)

并且更改

Geocoder geoCoder = new Geocoder(MapClass.this, Locale.getDefault());

to

Geocoder geoCoder = new Geocoder(MapClass.this);

谢谢你们所有人抽出时间来...

如何在哪里输入街道地址,然后是城市名称和州名? - DroidLearner
在我的问题中有 {String addressStr = "xxxx 你的地址在这里 xxxx"} === 你的地址必须是街道地址,然后是城市,然后是州,最后是国家。 - mohsin raza
什么是状态?并非所有国家都有状态。我能用国家名称替代吗? - Haya Raed
你离开状态,只需简单地放置你的地址。 - mohsin raza

1

这是我的解决方案:

private void createMarkerWithLocation() {
    /* Use the LocationManager class to obtain GPS locations */
    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 40, this);

    Location location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    /*check both providers even for lastKnownLocation*/
    if (location == null)
        location = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if(location != null) {

        double latitude = location.getLatitude();
        double longitude = location.getLongitude();

        LatLng currentLatLng = new LatLng(latitude, longitude);

        if(isConnected(this)) {
            Geocoder gCoder = new Geocoder(ChoiceDestinationActivity.this);
            List<Address> addresses = gCoder.getFromLocation(latitude, longitude, 1);
            String address = addresses.get(0).getAddressLine(0);
            String city = addresses.get(0).getAddressLine(1);
            String country = addresses.get(0).getAddressLine(2);
            Toast.makeText(this, country + ", " + city + ", " + address, Toast.LENGTH_SHORT).show();

            marker = map.addMarker(new MarkerOptions()
            .position(currentLatLng)
            .title(city)
            .snippet(address)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
        }
    }
}

public static boolean isConnected(Context context) {
    NetworkInfo ni = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    return (ni!=null && ni.isAvailable() && ni.isConnected());
}

我使用它在谷歌地图上添加标记。它可以让您检索有关用户位置的所有信息。
希望这有所帮助!

谢谢回复,但我正在从用户那里获取地址,然后我想在该点上放置标记...而不是从当前位置获取地址... - mohsin raza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接