2013-01-10 33 views
0

在我的應用程序中,當用戶插入數據我想捕捉用戶的確切位置。我嘗試了很多方法。但他們沒有一個顯示確切的地方。 (我的意思是它們只顯示國家和管理區域的詳細信息,我使用getLocality和getFeatureName,如果在該緯度和經度沒有找到位置或功能名稱,那麼它將返回空值)。我的代碼是我怎樣才能得到最近的地方(村或街道名稱)

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(狀態)。如果是,請解釋。

誰能幫助我,請

+0

它不是我的問題 – user1954492

+0

做地理編碼,讓您的位置的準確名稱 – itsrajesh4uguys

+0

嘗試與此一..在此您將獲得有關從位置獲得地址的詳細信息。 http://mobiforge.com/developing/story/using-google-maps-android – itsrajesh4uguys

回答

0

使用谷歌反向地理編碼API,您可以通過給緯度計算你實際的街道地址和lantitude值

Get Lat & Lan through GPS Receiver in Android

然後調用谷歌反向地理編碼API頂部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; 

    } 

} 
+0

謝謝。我會嘗試 – user1954492

+0

什麼是內容? – user1954492

+0

我已經編輯了我的答案內容,就是那個請求的答覆。 – Dinesh

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

}