0

在我的应用程序中,当用户插入数据时,我想捕获用户的确切位置。我试过很多方法。但他们都没有显示确切的位置。(我的意思是它们只显示国家和管理区域的详细信息。我使用了 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(州)。如果是,请解释。

谁能帮帮我

4

2 回答 2

0

使用谷歌反向地理编码 api,您可以通过提供纬度和纬度值来获取您的实际街道地址。

通过 Android 中的 GPS 接收器获取纬度和局域网

然后调用 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;

    }

}
于 2013-01-10T05:03:35.363 回答
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;
}

}

于 2014-07-07T12:03:35.313 回答