Getting address of a location from latitude and longitude in android

纵饮孤独 提交于 2020-02-07 05:48:04

问题


I am building an application. In which now i am getting the map and also the latitude and longitude of the touched location. I also wanted the address of the touched location corresponding to the latitude and longitude. How can i achieve this?

 public class MapViewEvents extends MapActivity {

 private TextView myLongitude, myLatitude;
 String result1;
 private Context context;
 private MapView myMapView;
 private MapController myMapController;
 private double la;
 private double lo;
@Override
protected void onCreate(Bundle icicle) {
    // TODO Auto-generated method stub
    super.onCreate(icicle);
    setContentView(R.layout.mymapview);

    myMapView = (MapView)findViewById(R.id.mapview);
    myMapController = myMapView.getController();  
    myMapView.setBuiltInZoomControls(true);

    myLongitude = (TextView)findViewById(R.id.longitude);
    myLatitude = (TextView)findViewById(R.id.latitude);

        la= RoamMeo_Config.Gpslatitude;
        lo=RoamMeo_Config.Gpslongitude;

        int latitude = (int)(la * 1000000);
        int longitude = (int)(lo * 1000000);

        GeoPoint initGeoPoint = new GeoPoint(latitude, longitude);
        CenterLocation(initGeoPoint);

        Button nxt_button = (Button) findViewById(R.id.btn_next);
        nxt_button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //System.out.println(result1);
                    //Intent intent = new Intent(Home.this, EventList.class);
                    //startActivity(intent);
            }
        });
}

private void placeMarker(int markerLatitude, int markerLongitude)
{
    Drawable marker=getResources().getDrawable(
            android.R.drawable.ic_menu_myplaces);
    marker.setBounds(0, 0, marker.getIntrinsicWidth(), 
            marker.getIntrinsicHeight());
    myMapView.getOverlays().add(new InterestingLocations(marker, 
            markerLatitude, markerLongitude));
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

private void CenterLocation(GeoPoint centerGeoPoint)
{
    myMapController.animateTo(centerGeoPoint);

    myLongitude.setText("Longitude: "+
        String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000));
    myLatitude.setText("Latitude: "+
        String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000));
    placeMarker(centerGeoPoint.getLatitudeE6(), 
            centerGeoPoint.getLongitudeE6());
};


class InterestingLocations extends ItemizedOverlay<OverlayItem>{

    private List<OverlayItem> locations = 
        new ArrayList<OverlayItem>();
    private Drawable marker;
    private OverlayItem myOverlayItem;

    boolean MoveMap;

    public InterestingLocations(Drawable defaultMarker, 
            int LatitudeE6, int LongitudeE6) {
        super(defaultMarker);
        // TODO Auto-generated constructor stub
        this.marker=defaultMarker;
        // create locations of interest
        GeoPoint myPlace = new GeoPoint(LatitudeE6,LongitudeE6);
        myOverlayItem = new OverlayItem(myPlace, "My Place", "My Place");
        locations.add(myOverlayItem);

        populate();
    }


    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return locations.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return locations.size();
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, 
            boolean shadow) {
        // TODO Auto-generated method stub
        super.draw(canvas, mapView, shadow);

        boundCenterBottom(marker);
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0, MapView arg1) {
        // TODO Auto-generated method stub
        //super.onTouchEvent(arg0, arg1);

        int Action = arg0.getAction();
        if (Action == MotionEvent.ACTION_UP){

            if(!MoveMap)
            {
                Projection proj = myMapView.getProjection(); 
                GeoPoint loc = proj.fromPixels((int)arg0.getX(), (int)arg0.getY());

                //remove the last marker
                myMapView.getOverlays().remove(0);

                CenterLocation(loc);
            }

        }
        else if (Action == MotionEvent.ACTION_DOWN){

            MoveMap = false;

        }
        else if (Action == MotionEvent.ACTION_MOVE){                
            MoveMap = true;
        }

        return super.onTouchEvent(arg0, arg1);
        //return false;
    }

I executed the below code and i'm getting this error printed in my

logcat:

11-09 15:00:46.318: W/System.err(664): java.io.IOException: Service not Available
11-09 15:00:46.469: W/System.err(664):  at android.location.Geocoder.getFromLocation(Geocoder.java:117)
11-09 15:00:46.469: W/System.err(664):  at com.project.r
oammeo.MapViewEvents$1.onClick(MapViewEvents.java:79)
11-09 15:00:46.469: W/System.err(664):  at android.view.View.performClick(View.java:2408)
11-09 15:00:46.469: W/System.err(664):  at android.view.View$PerformClick.run(View.java:8816)
11-09 15:00:46.478: W/System.err(664):  at android.os.Handler.handleCallback(Handler.java:587)
11-09 15:00:46.488: W/System.err(664):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 15:00:46.498: W/System.err(664):  at android.os.Looper.loop(Looper.java:123)
11-09 15:00:46.524: W/System.err(664):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-09 15:00:46.589: W/System.err(664):  at java.lang.reflect.Method.invokeNative(Native Method)
11-09 15:00:46.641: W/System.err(664):  at java.lang.reflect.Method.invoke(Method.java:521)
11-09 15:00:46.641: W/System.err(664):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-09 15:00:46.660: W/System.err(664):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

11-09 15:00:46.660: W/System.err(664):  at dalvik.system.NativeStart.main(Native Method)

回答1:


To get the address from the lat, long,

Try this code,

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);

EDIT:

   String addressString;

try {
  Geocoder geocoder = new Geocoder(this, Locale.getDefault());
  List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
  StringBuilder sb = new StringBuilder();
  if (addresses.size() > 0) {
    Address address = addresses.get(0);

    sb.append(address.getLocality()).append("\n");
    sb.append(address.getCountryName());
  }

  addressString = sb.toString();

  Log.e("Address from lat,long ;", addressString);
 } catch (IOException e) {}
}

For exception :

java.io.IOException: Service not Available
at android.location.Geocoder.getFromLocation(Geocoder.java:117)

It's a bug in the emulator for 2.2 http://code.google.com/p/android/issues/detail?id=8816

also here: http://groups.google.com/group/android-developers/browse_thread/thread/b02c29d746471358



来源:https://stackoverflow.com/questions/8062126/getting-address-of-a-location-from-latitude-and-longitude-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!