我怎样才能得到最近的地方(村庄或街道名称)

时间:2013-01-10 04:45:46

标签: android gps geocoding google-places-api reverse-geocoding

在我的应用程序中,当用户插入数据时,我想捕获用户的确切位置。我尝试了很多方法。但他们都没有显示出确切的位置。 (我的意思是它们只显示国家和管理区域的详细信息。我使用了getLocality和getFeatureName。如果在该纬度和经度上没有找到Locality或Featurename,那么它将返回一个空值)。我的代码是

Geocoder coder=new Geocoder(MainActivity.mContext,Locale.getDefault());
    try
    {
        List<Address> listaddress=coder.getFromLocation(lat, lon, 1);
        Address obj=listaddress.get(0);
        String add=obj.getAddressLine(0);
        add = add+"\n"+obj.getLatitude();
        add = add+","+obj.getLongitude();
        add = add + "," + obj.getCountryName();
        add = add + "," + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();//null
        add = add + "," + obj.getSubAdminArea();
        add = add +","+obj.getFeatureName();//null
        add = add+ "," +obj.getPremises();//null
        add = add + "\n" + obj.getLocality();//null
        add = add + "," + obj.getSubThoroughfare();//null
        add = add+ "," +obj.getSubLocality();//null
        add = add+"\n"+obj.getThoroughfare();

        Log.v("IGA", "Address" + add);
        //Toast.makeText(this, "Address=>" + add,Toast.LENGTH_SHORT).show();
        t.setText(" "+add);


    }

所以我不知道如何解决它。但我有一个想法。我需要找到最接近我确切纬度和经度值的地方,以便我可以使用那个地方。但我不知道如何找到最近的地方(最近的地方意味着村庄或街道,而不是其他任何地方)。

此外,在Android手机中,有一个应用程序“Places”就在那里。它显示了关于我在哪里的正确区域。是否有可能使用应用程序“位置”来查找我的确切或最近的区域。 (我需要最近的村庄或街道,而不是subAdminArea(州)。如果是,请解释。

任何人都可以帮助我

2 个答案:

答案 0 :(得分:0)

使用谷歌反向地理编码API,你可以通过给出纬度和经度值来获得你的实际街道地址。

Get Lat & Lan through GPS Receiver in Android

然后拨打google reverse geocoding api top fecth实际的街道地址:

  MapManager in = new MapManager(
                    "http://maps.google.com/maps/geo?output=xml&oe=utf-8&ll="
                            + lattitude                             + "%2C"
                            + lantitude                         + "&key=get-key-from-google-map-api");
            content = in.URLRequest();
            System.out.println("Addrss:"+in.parseMapData(content));

Class MapManager{
  String url ="";

  MapManager(String url){
         this.url = url;
  }

  public String parseMapData(String data) {
            String addr = "";
            try {
                InputStream in = new ByteArrayInputStream(data.getBytes());
                DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                        .newDocumentBuilder();
                Document doc = builder.parse(in, null);
                NodeList address = doc.getElementsByTagName("address");
                addr = address.item(0).getTextContent();
            } catch (Throwable t) {
                Log.v("Exception", "Exception: " + t.toString());
            }
            return addr;
        }



     public String URLRequest() {

        httpclient = new DefaultHttpClient();
        try {
            httpget = new HttpGet(url);
            httpresponse = httpclient.execute(httpget);
            httpentity = httpresponse.getEntity();
            response = EntityUtils.toString(httpentity);
            response = response.trim();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            IsServerConn = false;
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            IsServerConn = false;
        }
        return response;

    }

}

答案 1 :(得分:0)

   private class MatchingNearByLocationTask extends
        AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        progressDialog = new ProgressDialog(getContext());
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(true);
        progressDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {

        jsonStr = getLocationInfo(latitude, longitude).toString(); // put longitude and      latitude value from which u want find nearer palce
        if (jsonStr != null) {
            Log.i("location--??", jsonStr);

            JSONObject jsonObj;
            try {
                jsonObj = new JSONObject(jsonStr);

                JSONObject responseJsonObject = jsonObj
                        .getJSONObject("response");

                JSONArray venues = responseJsonObject
                        .getJSONArray(("venues"));

                for (int index = 0; index < venues.length(); index++) {

                    locationObject = new NearByLocationObject();

                    String id = "", name = "", longitude = "", latitude = "";

                    JSONObject venuesJsonObj = venues.getJSONObject(index);

                    id = venuesJsonObj.getString("id");
                    name = venuesJsonObj.getString("name");

                    JSONObject latLngJsonObj = venuesJsonObj
                            .getJSONObject("location");

                    longitude = latLngJsonObj.getString("lng");
                    latitude = latLngJsonObj.getString("lat");

                    locationObject.setId(id);
                    locationObject.setNameOfLocation(name);
                    locationObject.setLocationOfLatitude(latitude);
                    locationObject.setLocationOfLongitude(longitude);

                    nearByLocationArrayList.add(locationObject);

                }

            } catch (JSONException e) {

                e.printStackTrace();
            }

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }

        adapter = new NearByLocationArrayAdapter(getContext(),
                R.layout.nearby_location_item, nearByLocationArrayList);
        nearByLocationListView.setAdapter(adapter);
    }

    @Override
    protected void onCancelled() {

        super.onCancelled();
        progressDialog.dismiss();

    }

}

private JSONObject getLocationInfo(double lat, double lng) {

    HttpGet httpGet = new HttpGet(
            "https://api.foursquare.com/v2/venues/search?ll="
                    + lat
                    + ","
                    + lng
                    + "&radius=100&oauth_token=TNFKWLITLCJYWPSLAXQNHCSDPHZ4IS5PWVDD45OI224JGFFM&v=20140407&intent=checkin");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    try {
        response = client.execute(httpGet);
        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) {
        e.printStackTrace();
    }

    return jsonObject;
}



public class NearByLocationObject {

String id = "", nameOfLocation, locationOfLatitude, LocationOfLongitude;

public NearByLocationObject() {
    super();
    // TODO Auto-generated constructor stub
}

public NearByLocationObject(String id, String nameOfLocation,
        String locationOfLatitude, String locationOfLongitude) {
    super();
    this.id = id;
    this.nameOfLocation = nameOfLocation;
    this.locationOfLatitude = locationOfLatitude;
    LocationOfLongitude = locationOfLongitude;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getNameOfLocation() {
    return nameOfLocation;
}

public void setNameOfLocation(String nameOfLocation) {
    this.nameOfLocation = nameOfLocation;
}

public String getLocationOfLatitude() {
    return locationOfLatitude;
}

public void setLocationOfLatitude(String locationOfLatitude) {
    this.locationOfLatitude = locationOfLatitude;
}

public String getLocationOfLongitude() {
    return LocationOfLongitude;
}

public void setLocationOfLongitude(String locationOfLongitude) {
    LocationOfLongitude = locationOfLongitude;
}

}