如何從谷歌地圖,當我點擊一個特定的地址得到的位置或地址。是否可以使用地圖疊加從地圖視圖中收集地址。谷歌地圖:如何獲取地址在Android
2
A
回答
9
請使用下面的代碼獲取地址。
try {
Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.isEmpty()) {
yourtextfieldname.setText("Waiting for Location");
}
else {
if (addresses.size() > 0) {
yourtextfieldname.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
//Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
}
}
}
catch (Exception e) {
e.printStackTrace(); // getFromLocation() may sometimes fail
}
,看看下面的鏈接瞭解更多信息和完整的例子。
+0
謝謝@Dipak ... –
2
喜它的簡單是指該鏈接Click Here
import java.io.IOException;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MainActivity extends MapActivity {
private MapView mapView;
private TextView tvLocation;
// Handles Taps on the Google Map
Handler h = new Handler(){
// Invoked by the method onTap()
// in the class CurrentLocationOverlay
@Override
public void handleMessage(Message msg) {
Bundle data = msg.getData();
// Getting the Latitude of the location
int latitude = data.getInt("latitude");
// Getting the Longitude of the location
int longitude = data.getInt("longitude");
// Show the location in the Google Map
showLocation(latitude,longitude);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to map_view available in activity_main.xml
mapView = (MapView) findViewById(R.id.map_view);
// Getting reference to tv_location available in activity_main.xml
tvLocation = (TextView) findViewById(R.id.tv_location);
// Default Latitude
int latitude = 28426365;
// Default Longitude
int longitude = 77320393;
// Show the location in the Google Map
showLocation(latitude,longitude);
}
private void showLocation(int latitude, int longitude){
// Setting Zoom Controls
mapView.setBuiltInZoomControls(true);
// Getting the MapController
MapController mapController = mapView.getController();
// Getting Overlays of the map
List<Overlay> overlays = mapView.getOverlays();
// Getting Drawable object corresponding to a resource image
Drawable drawable = getResources().getDrawable(R.drawable.marker);
// Creating an ItemizedOverlay
TouchedLocationOverlay locationOverlay = new TouchedLocationOverlay(drawable,h);
// Getting the MapController
MapController mc = mapView.getController();
// Creating an instance of GeoPoint, to display in Google Map
GeoPoint p = new GeoPoint(
latitude,
longitude
);
// Locating the point in the Google Map
mc.animateTo(p);
// Creating an OverlayItem to mark the point
OverlayItem overlayItem = new OverlayItem(p, "Item", "Item");
// Adding the OverlayItem in the LocationOverlay
locationOverlay.addOverlay(overlayItem);
// Clearing the overlays
overlays.clear();
// Adding locationOverlay to the overlay
overlays.add(locationOverlay);
// Redraws the map
mapView.invalidate();
Double[] lat_long = new Double[] { latitude/1E6, longitude/1E6 };
// Executing ReverseGeocodingTask to get Address
new ReverseGeocodingTask(getBaseContext()).execute(lat_long);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return false;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private class ReverseGeocodingTask extends AsyncTask<Double, Void, String>{
Context mContext;
public ReverseGeocodingTask(Context context){
super();
mContext = context;
}
@Override
protected String doInBackground(Double... params) {
Geocoder geocoder = new Geocoder(mContext);
double latitude = params[0].doubleValue();
double longitude = params[1].doubleValue();
List<Address> addresses = null;
String addressText="";
try {
addresses = geocoder.getFromLocation(latitude, longitude,1);
} catch (IOException e) {
e.printStackTrace();
}
if(addresses != null && addresses.size() > 0){
Address address = addresses.get(0);
addressText = String.format("%s, %s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getLocality(),
address.getCountryName());
}
return addressText;
}
@Override
protected void onPostExecute(String addressText) {
// Setting address of the touched Position
tvLocation.setText(addressText);
}
}
}
相關問題
- 1. 如何從谷歌地圖應用獲取地址?
- 2. 如何從谷歌地圖api獲取地址lat和lon?
- 3. 如何獲取谷歌地圖上的點的完整地址?
- 4. 從谷歌地圖提取地址
- 5. 如何獲取動態谷歌地圖網址(有靜態地圖網址)
- 6. 如何讓解析網址在谷歌地圖獲得Android中
- 7. 如何在webview中顯示谷歌地圖導航地址android
- 8. 如何獲取您的圖片網址磁貼? (谷歌地圖)
- 9. Android:從谷歌地圖獲取路線
- 10. 獲取路線谷歌地圖Android
- 11. 地理編碼地址 - 獲取某個地址(谷歌API)
- 12. Android:從谷歌地圖V2提供地址提供地址
- 13. 困惑 - 如何獲取地址的地理編碼(谷歌地圖)
- 14. 谷歌地圖在html中的地址
- 15. Android和谷歌地圖 - 如何存儲街道地址
- 16. 如何使谷歌地圖上的地址搜索Android薑餅?
- 17. 使用GPS和谷歌地圖在Android中獲取街道地址
- 18. Android谷歌地圖:錯誤在Android谷歌地圖
- 19. 如何獲得地址的緯度和經度?谷歌地圖
- 20. 獲取谷歌地圖返回地方
- 21. rails從谷歌地圖使用經緯度獲取地址
- 22. 谷歌地圖API - 獲取用戶地址
- 23. 獲取谷歌地圖時,點擊地址
- 24. 谷歌地圖 - 獲取附近地址列表
- 25. Sencha Touch谷歌地圖獲取地址名稱
- 26. iphone谷歌地圖獲取地址的經度和緯度
- 27. 從谷歌地圖API中獲取latlng地址
- 28. 從谷歌地圖經緯度獲取位置地址
- 29. 從谷歌地圖獲取地址和座標
- 30. 從谷歌地圖獲取地址api v3英文
belove鏈接幫助你如何從谷歌地圖獲得位置的地址..try它 http://stackoverflow.com/questions/2084065/get-map-address-or-location-address-in-android – Hemant