开发者

How to check position with respect to lat and long

If I have a lat and long of a particular place, how can I check that the place is in a particular st开发者_JAVA百科ate.

For Example: If lat and long refer to Santa Clara, how can I check that it is in California?

Thanks


use the Geocoder and Address Classes to acheive the solution. this code may help u.. all the best...

Geocoder gc = new Geocoder(this, Locale.getDefault()); 
   try { 
     List<Address> addresses = gc.getFromLocation(lat, lng, 1); 
     StringBuilder sb = new StringBuilder(); 
     if (addresses.size() > 0) { 
        Address address = addresses.get(0); 

        for (int i = 0; i < address.getMaxAddressLineIndex(); i++) 
          String addr=address.getAddressLine(i); 
          //use this addr string and compare . u will get the solution

     } 

   } catch (IOException e) {} 


Use Geocoder class to get address from your lat/lon. Country should be part of the address.

Note: this does not work for places that do not have addresses (country, woods, etc..).

Update:

There is a nice service that uses Google Maps reverse-geocoding database (same that Android uses internally): http://reverse-geocoding.appspot.com/

Poking around actually gives country data for any point (even middle of Greenland) - so you should be ok.


Try using following code

Geocoder gc = new Geocoder(this, Locale.getDefault()); 
   try { 
     List<Address> addresses = gc.getFromLocation(lat, lng, 1); 
     if (addresses.size() > 0) { 
        Address address = addresses.get(0); 

          String state =  address.getLocality();
     }

   } catch (IOException e) {} 


public static String getUserAddress(String lat, String lon) {
        String userlocation = null;
        String userCountry = null;
        String userCountryCode = null;
        String readUserFeed = readUserLocationFeed(lat.trim() + ","+ lon.trim());
        try {
            JSONObject Strjson = new JSONObject(readUserFeed);
            JSONArray jsonArray = new JSONArray(Strjson.getString("results"));
            JSONArray  array = new JSONArray(jsonArray.getJSONObject(0).getString("address_components"));
            userCountry = array.getJSONObject(2).getString("long_name").toString();
            userCountryCode = array.getJSONObject(2).getString("short_name").toString();
            userlocation = jsonArray.getJSONObject(1).getString("formatted_address").toString();
            System.out.println("User Address : "+userlocation +"\nUser Country : "+userCountry+"\nUser CountryCode : "+ userCountryCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.i("User Location ", userlocation);
        return userlocation;
    }

    public static String readUserLocationFeed(String address) {
        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+ address + "&sensor=false");
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
            } else {
                Log.e(ReverseGeocode.class.toString(), "Failed to download file");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }


Three things in my answer bag:

  1. Reverse Geocoding in android: http://www.smnirven.com/?p=39
  2. Foursquares API
  3. Maintaining your own database.

For you the first one is the quick and easiest and the third one is the most reliable and custom.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜