得到纬度和来自android地址的经度

时间:2013-05-22 07:58:18

标签: android geocoding android-maps android-maps-v2

我试图获得纬度和来自地址的经度,问题是......当我只提供城市名称而不是它给我正确的纬度&经度,当我提供完整的地址(如州,城市名称,街道号),而不是给我正确的纬度&经度...

谢谢你的合作回应...

我的代码是..

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

2 个答案:

答案 0 :(得分:6)

我不喜欢...... :)

只有序列不正确...

首先提供街道地址而不是城市名称而不是州...它从地址给我正确的纬度和经度.. :)

并更改

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

Geocoder geoCoder = new Geocoder(MapClass.this);

谢谢你们所有人的时间......

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

我用它在谷歌地图上添加一个标记。它允许您检索有关用户位置的所有信息。

我希望你有所帮助!