无法在地图上显示我的位置?

时间:2012-03-04 12:39:46

标签: android google-maps gps overlays

未显示叠加层且未指定位置
将叠加层和地图代码从onCreat()方法更改为updateOverlays(),我只想获得正确的位置并在地图上正确设置叠加层

    public class tabsActivity extends MapActivity
    {
    private static final String LIST_TAB_TAG = "Notification";
    private static final String MAP_TAB_TAG = "Map";

    private TabHost tabHost;
    private ListView listView;
    private MapView mapView;
    MyLocationOverlay Compass;
    double longitude , latitude;
    MapController mc;
    GeoPoint point;

    protected LocationManager locationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maps_notification_tabs);

        LocationListener listener = new LocationListener() 
        {

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) 
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) 
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) 
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLocationChanged(Location location) 
            {
                updateOverlays(location);
            }
        };

        LocationManager locMgr = (LocationManager) getBaseContext().getSystemService(Context.LOCATION_SERVICE);
        locMgr.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, listener);

        // setup map view
        mapView = (MapView) findViewById(R.id.mapview);


        //Compass Setup
        Compass = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(Compass);
        final MapController control = mapView.getController();



    }

    public void updateOverlays(Location location)
    {
        mapView.setBuiltInZoomControls(true);
        mapView.postInvalidate();
        mapView.setSatellite(true);
        mc = mapView.getController();

        point = new GeoPoint((int) location.getLatitude() , (int) location.getLongitude());
        mc.animateTo(point);
        mc.setZoom(10); 
        mapView.invalidate();

        OverlayItem overlayitem = new OverlayItem(point, "Hint", "Your Are Here");

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable =   this.getResources().getDrawable(R.drawable.black);
        MapOverlays itemizedoverlay = new MapOverlays(drawable, this);
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);
    }

1 个答案:

答案 0 :(得分:1)

您的问题很容易解释:您只使用在程序仍处于onCreate()方法时所获得的位置。

您需要从侦听器更新叠加层。因此,请创建一个可以从更新叠加层的LocationListener调用的单独方法。

编辑:

基本上这样做(不完整,但应该给你的想法!)

public class MyMapActivity extends MapActivity {
    MapView mapView;
    LocationListener locListener;

    public onCreate() {
        // setup your map
        mapView = findViewById(R.id.my_map);
        // setup listener
        locListener = new LocationListener() {
            // override methods
            public void onLocationChanged(Location location) {
                updateOverlays(location);
            }
        }
    }

    public void updateOverlays(Location location) {
        // this is basically your code just a bit modified (removed unnecessary code and added new code)
        mc = mapView.getController();
        p = new GeoPoint(location.getLatitudeE6(), location.getLongitudeE6());

        mc.animateTo(p);
        mc.setZoom(10); 
        mapView.invalidate();

        // remove all existing overlays!
        List<Overlay> mapOverlays = mapView.getOverlays();
        mapOverlays.clear();

        //Compass Setup
        Compass = new MyLocationOverlay(this, mapView);
        mapOverlays.add(Compass);

        Drawable drawable = getResources().getDrawable(R.drawable.black);
        MapOverlays itemizedoverlay = new MapOverlays(drawable, this);

        OverlayItem overlayitem = new OverlayItem(p, "Hint", "Your Are Here");
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);
    }
}

EDIT2:

您的GeoPoint计算出错。你不给1E6整数,你只给一些小的双打。变化

point = new GeoPoint((int) location.getLatitude() , (int) location.getLongitude());

point = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));