0

オーバーレイが表示されず、位置が指定されていません
オーバーレイとマップ コードを 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);
    }
4

1 に答える 1

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

編集2:

GeoPoint の計算にエラーがあります。1E6 整数を与えるのではなく、いくつかの小さな double を与えるだけです。変化する

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

point = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
于 2012-03-04T13:32:59.493 に答える