2012-11-28 42 views
2

如何從谷歌地圖,當我點擊一個特定的地址得到的位置或地址。是否可以使用地圖疊加從地圖視圖中收集地址。谷歌地圖:如何獲取地址在Android

+0

belove鏈接幫助你如何從谷歌地圖獲得位置的地址..try它 http://stackoverflow.com/questions/2084065/get-map-address-or-location-address-in-android – Hemant

回答

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 
} 

,看看下面的鏈接瞭解更多信息和完整的例子。

Using Google Maps in Android

+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); 
     } 
    } 
} 
相關問題