如何从地址名称中获取纬度和经度



我有一个地址名,我想从我的应用程序中获取经纬度,我参考了一些教程,最后还是无法成功获取。

日志值为空。

我错过了哪一步? 任何帮助将不胜感激,谢谢。

全局变量:

double editLatitude, editLongitude;

我的地理编码器:

 Geocoder coder = new Geocoder(getActivity());
            List<Address> address;
            try {
                address = coder.getFromLocationName("Las Vegas", 5);
                if (address == null) {
                    return null;
                }
                Address location = address.get(0);
                editLatitude = location.getLatitude();
                editLongitude = location.getLongitude();
                Log.d("editLatitude", editLatitude + ">>>>>>>>>>>>>>>>>>");
                Log.d("editLongitude", editLongitude + ">>>>>>>>>>>>>>>>>>");
            } catch (IOException ex) {
                ex.printStackTrace();
            }

我在这里设置了纬度和经度:

 @Override
        public void onLocationChanged(Location location) {
            mLastLocation = location;
            if (mCurrLocationMarker != null) {
                mCurrLocationMarker.remove();
            }
            //Place current location marker
            //LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
            //LatLng latLng = new LatLng(22.751262, 121.140801);
//---------------------------------------------
            LatLng latLng = new LatLng(editLatitude, editLongitude);
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("Current Position");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
            //move map camera
            mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(16));
            //optionally, stop location updates if only current location is needed
            if (mGoogleApiClient != null) {
                LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
            }
        }

创建一个返回带有 HTTP 调用响应的 JSONObject 的方法,如下所示

public static JSONObject getLocationInfo(String address) {
    StringBuilder stringBuilder = new StringBuilder();
    try {
    address = address.replaceAll(" ","%20");    
    HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    stringBuilder = new StringBuilder();

        response = client.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return jsonObject;
}   

现在将 JSONObject 传递给 getLatLong() 方法,如下所示

public static boolean getLatLong(JSONObject jsonObject) {
    try {
        longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lng");
        latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lat");
    } catch (JSONException e) {
        return false;
    }
    return true;
}

试试这个

double Lat = geocoder.getFromLocationName(address_name, 1).get(0).getLatitude();
double Lon = geocoder.getFromLocationName(address_name, 1).get(0).getLongitude();

最新更新