简体   繁体   English

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

[英]How to get Latitude and Longitude from address name

I have a address name , i want to get the Latitude and Longitude from my app , i reference some tutorial , i still can't get it successfully in the end. 我有一个地址名称,我想从我的应用程序中获取纬度和经度,我参考了一些教程,但最终还是无法成功获取。

The log value is empty. 日志值为空。

What step i miss it ? 我错过了哪一步? any help would be grateful,thanks. 任何帮助将不胜感激,谢谢。

global variable: 全局变量:

double editLatitude, editLongitude;

my Geocoder: 我的地址解析器:

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

i set the latitude and longitude over here: 我在这里设置纬度和经度:

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

create a method that returns a JSONObject with the response of the HTTP Call like following 创建一个返回JSONObject和HTTP调用响应的方法,如下所示

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

now pass that JSONObject to getLatLong() method like following 现在将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;
}

try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM