【问题标题】:Reverse Geocoding not working to get the address反向地理编码无法获取地址
【发布时间】:2012-03-13 10:53:18
【问题描述】:

我已经为谷歌地图编写了代码,它工作正常并显示位置。现在我正在尝试反向地理编码,以便我可以获得纬度和经度坐标的地址,但它不起作用。

我的代码是

public class SelectLocation extends MapActivity {
MapView mapView; 
MapController mc;
GeoPoint p;
String coordinates[];


class MapOverlay extends com.google.android.maps.Overlay
{

    private GeoPoint p; 
    public MapOverlay(GeoPoint p){
        this.p = p;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, 
            boolean shadow, long when) 
    {
        super.draw(canvas, mapView, shadow);                   

        //---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);

        //---add the marker---
        Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.pushpin);            
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
        return true;
    }   

    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    {   
        //---when user lifts his finger---
        if (event.getAction() == 1) {                
            GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());

            Toast.makeText(getBaseContext(), 
                    p.getLatitudeE6() / 1E6 + "," + 
                            p.getLongitudeE6() /1E6 , 
                            Toast.LENGTH_SHORT).show();
            Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());
                try {
                    List<Address> addresses = geoCoder.getFromLocation(
                        p.getLatitudeE6()  / 1E6, 
                        p.getLongitudeE6() / 1E6, 1);

                    String add = "";
                    if (addresses.size() > 0) 
                    {
                        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                             i++)
                           add += addresses.get(0).getAddressLine(i) + "\n";
                    }

                    Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                }
                catch (IOException e) {                
                    e.printStackTrace();
                }   
                return true;
            }
            mapView.getOverlays().clear();
            mapView.invalidate();
            mapView.getOverlays().add(new MapOverlay(p));
            Intent i=new Intent(SelectLocation.this,SetLocat.class);
            startActivity(i);

        return false;
    }        

}
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selectlocation);

    mapView = (MapView) findViewById(R.id.mapView);

    mapView.setBuiltInZoomControls(true);

    mc = mapView.getController();
    String coordinates[] = {"1.352566007", "103.78921587"};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));

    mc.animateTo(p);
    mc.setZoom(17);
    MapOverlay mapOverlay = new MapOverlay(p);


    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay);        




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

}

【问题讨论】:

  • 试试getApplicationContext而不是getBaseContext...它可以解决你的问题...
  • 不显示错误但显示纬度和经度坐标不显示任何位置名称
  • 试一下我的代码,因为它在我的项目中工作......
  • 您是在模拟器中运行它吗?如果是,哪个 API 级别?
  • 是的,正在模拟器 API 级别 10 上运行它

标签: android maps geocoding


【解决方案1】:
//
        //  Write the location name.
        //

        try {

            Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
            List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
            if (addresses.isEmpty()) {
                addres.setText("Waiting for Location");
            }
            else {
                if (addresses.size() > 0) {
                    addres.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
        }

我已经在我的项目中编写了这段代码,它运行良好。 将此与您的代码进行比较。希望这会对你有所帮助....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 2012-08-16
    • 2014-12-17
    • 1970-01-01
    相关资源
    最近更新 更多