Android:不读取反向地理编码的输入地址

时间:2011-11-18 11:56:53

标签: android google-maps

这是我的MapActivity类,它应该从字符串中读取地址,将其反转为经度&纬度并用标记在地图上显示地址。

段:

 Geocoder geoCoder = new Geocoder(this, Locale.getDefault()); 
 try {
        List<Address> addresses = geoCoder.getFromLocationName(
                            getString(R.string.address), 5);
                    String add = "";
                    if (addresses.size() > 0) {
                        p = new GeoPoint(
                                (int) (addresses.get(0).getLatitude() * 1E6), 
                                (int) (addresses.get(0).getLongitude() * 1E6));
                        mc.animateTo(p);
                        mc.setZoom(17);

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

                        mapView.invalidate();

                    }    
                } catch (IOException e) {
                    e.printStackTrace();
                }

不知怎的,它不起作用,它没有显示给定的地址,也没有标记。 任何人都可以看到我的错误在哪里?

全班:

public class NewMapsActivity extends MapActivity 
    {    

        protected boolean isRouteDisplayed() {
            return false;
        }

        private MapView myMap;
        private Geocoder gc;
        private double lat;
        private double lon;
        MapView mapView;
        MapController mc;
        GeoPoint p;


        class MapOverlay extends com.google.android.maps.Overlay
        {
            @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;
            }
        } 

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map);

            mapView = (MapView) findViewById(R.id.mapView);
            LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
            View zoomView = mapView.getZoomControls(); 

            zoomLayout.addView(zoomView, 
                new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, 
                    LayoutParams.WRAP_CONTENT)); 
            mapView.displayZoomControls(true);


            mc = mapView.getController();

            Geocoder geoCoder = new Geocoder(this, Locale.getDefault());    
            try {
                List<Address> addresses = geoCoder.getFromLocationName(
                        getString(R.string.address), 5);
                String add = "";
                if (addresses.size() > 0) {
                    p = new GeoPoint(
                            (int) (addresses.get(0).getLatitude() * 1E6), 
                            (int) (addresses.get(0).getLongitude() * 1E6));
                    mc.animateTo(p);
                    mc.setZoom(17);

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

                    mapView.invalidate();

                }    
            } catch (IOException e) {
                e.printStackTrace();
            }

1 个答案:

答案 0 :(得分:0)

问题解决了,它正在我的手机上工作,问题就在模拟器上