get latitude and longitude from city name

后端 未结 3 1269
面向向阳花
面向向阳花 2020-12-16 07:07

In my current android application, I would like to get the geocordinates based on an entered city name, street name or zip code. How can I accomplish this?

Best Rega

3条回答
  •  伪装坚强ぢ
    2020-12-16 07:50

    get geo co-ordinates via net

    private 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) {
            Log.i("getLocationInfo ClientProtocolException", e.toString());
        } catch (IOException e) {
    
            Log.i("getLocationInfo IOException", e.toString());
        }
    
    
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            Log.i("getLocationInfo JSONException", e.toString());
        }
    
        return jsonObject;
    }
    
    private static boolean getLatLong(JSONObject jsonObject) 
    {
    
        try {
    
           longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lng");
            Log.i("Log1", longitute1+"");
        latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");
            Log.i("lat1", latitude1+"");
        } catch (JSONException e) {
    
            longitute=0;
            latitude = 0;
            Log.i("getLatLong", e.toString());
            return false;
    
        }
    
        return true;
    }
    

提交回复
热议问题