在android中从纬度和经度获取位置的地址

时间:2011-11-09 08:28:31

标签: android google-maps location street-address

我正在构建一个应用程序。现在我正在获取地图以及触摸位置的纬度和经度。我还想要与纬度和经度对应的触摸位置的地址。我怎样才能做到这一点?

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

我执行了以下代码,我在

中打印出此错误

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 个答案:

答案 0 :(得分:5)

从lat获取地址,长,

试试这段代码,

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

修改

   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) {}
}

例外:

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

这是2.2 模拟器中的错误 http://code.google.com/p/android/issues/detail?id=8816

也在这里:http://groups.google.com/group/android-developers/browse_thread/thread/b02c29d746471358