2011-07-02 101 views
1

我正在使用OSMdroid。OverlayItem不更新位置更改(OSM)

我已經管理感謝這篇文章Adding Overlay to OSMDROID創造者將靜態項目覆蓋在特定位置。

現在我正在試圖讓地圖變得更有活力。所以當位置改變OverLayItem(標記地圖上的特定位置)也會自行更新。

這是我試圖做的。地圖確實會更新,但OverLayItem不會。

代碼:

package com.test.overlay; 

import java.util.ArrayList; 
import org.osmdroid.DefaultResourceProxyImpl; 
import org.osmdroid.ResourceProxy; 
import org.osmdroid.tileprovider.tilesource.TileSourceFactory; 
import org.osmdroid.util.GeoPoint; 
import org.osmdroid.views.MapController; 
import org.osmdroid.views.MapView; 
import org.osmdroid.views.overlay.ItemizedIconOverlay; 
import org.osmdroid.views.overlay.ItemizedOverlay; 
import org.osmdroid.views.overlay.OverlayItem; 
import org.osmdroid.views.util.constants.MapViewConstants; 

import android.app.Activity; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.Toast; 

public class SampleWithMinimapItemizedoverlay extends Activity implements 
     LocationListener, MapViewConstants 
{ 

    private MapView mMapView; 
    private MapController mapController; 
    private LocationManager mLocMgr; 
    private ItemizedOverlay<OverlayItem> mMyLocationOverlay; 
    private ResourceProxy mResourceProxy; 
    int longtitude = 31987968; 
    int latitude = 34783155; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext()); 
     setContentView(R.layout.main); 

     mMapView = (MapView) this.findViewById(R.id.mapview); 
     mMapView.setTileSource(TileSourceFactory.MAPNIK); 
     mMapView.setBuiltInZoomControls(true); 
     mMapView.setMultiTouchControls(true); 
     mapController = this.mMapView.getController(); 
     mapController.setZoom(15); 
     GeoPoint point2 = new GeoPoint(longtitude, latitude); // centre map here 
     GeoPoint point3 = new GeoPoint(longtitude + 2000, latitude + 2000); // icon 
                      // goes 
                      // here 
     // 31.987968,34.783155 

     mapController.setCenter(point2); 
     mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE); 
     mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100, 
       this); 

     ArrayList<OverlayItem> items = new ArrayList<OverlayItem>(); 
     // Put overlay icon a little way from map centre 
     items.add(new OverlayItem("Here", "SampleDescription", point3)); 

     /* OnTapListener for the Markers, shows a simple Toast. */ 
     this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items, 
       new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() 
       { 
        @Override 
        public boolean onItemSingleTapUp(final int index, 
          final OverlayItem item) 
        { 
         Toast.makeText(SampleWithMinimapItemizedoverlay.this, 
           "Item '" + item.mTitle, Toast.LENGTH_LONG) 
           .show(); 
         return true; // We 'handled' this event. 
        } 

        @Override 
        public boolean onItemLongPress(final int index, 
          final OverlayItem item) 
        { 
         Toast.makeText(SampleWithMinimapItemizedoverlay.this, 
           "Item '" + item.mTitle, Toast.LENGTH_LONG) 
           .show(); 
         return false; 
        } 
       }, mResourceProxy); 
     this.mMapView.getOverlays().add(this.mMyLocationOverlay); 
     mMapView.invalidate(); 
    } 

    -------//here I tried to do the change !!!!!!!!!--------------- 
    -- The map location being updated but not the overlayItem. 
    public void onLocationChanged(Location location) 
    { 
     latitude = (int) (location.getLatitude() * 1E6); 
     longtitude = (int) (location.getLongitude() * 1E6); 
     Toast.makeText(SampleWithMinimapItemizedoverlay.this, 
       "Location changed. Lat:" + latitude + " long:" + longtitude , 
       Toast.LENGTH_LONG).show(); 
     GeoPoint gpt = new GeoPoint(latitude, longtitude); 
     mapController.setCenter(gpt); 
     mMapView.invalidate(); 
    } 

    @Override 
    public void onProviderDisabled(String arg0) 
    { 
    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
    } 

} 

謝謝,

射線。

+0

當您更改位置時,您並未更改圖標的位置。您將不得不在onLocationChanged中執行此操作。這意味着你必須使GestureListener成爲一個非匿名類,即給它一個名字,因爲你需要再次引用它。我會發布一些修改後的代碼。 – NickT

回答

3

這應該工作OK

public class SampleWithMinimapItemizedoverlay extends Activity implements 
     LocationListener, MapViewConstants { 

    private MapView mMapView; 
    private MapController mapController; 
    private LocationManager mLocMgr; 
    private ItemizedOverlay<OverlayItem> mMyLocationOverlay; 
    private ResourceProxy mResourceProxy; 
    int mLongtitude = 31987968; 
    int mLatitude = 34783155; 
    ArrayList<OverlayItem> mItems; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext()); 
     setContentView(R.layout.main); 

     mMapView = (MapView) this.findViewById(R.id.mapview); 
     mMapView.setTileSource(TileSourceFactory.MAPNIK); 
     mMapView.setBuiltInZoomControls(true); 
     mMapView.setMultiTouchControls(true); 
     mapController = this.mMapView.getController(); 
     mapController.setZoom(15); 
     GeoPoint point2 = new GeoPoint(mLongtitude, mLatitude); // centre map here 
     GeoPoint point3 = new GeoPoint(mLongtitude + 2000, mLatitude + 2000); // icon 
                      // goes 
     // 31.987968,34.783155 
     mapController.setCenter(point2); 
     mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE); 
     mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100, 
       this); 

     mItems = new ArrayList<OverlayItem>(); 
     // Put overlay icon a little way from map centre 
     mItems.add(new OverlayItem("Here", "SampleDescription", point3)); 

     /* OnTapListener for the Markers, shows a simple Toast. */ 
     // REFER TO THE GESTURE LISTEMER BY NAME NOW 
     this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(mItems, 
       new Glistener() , mResourceProxy); 
     this.mMapView.getOverlays().add(this.mMyLocationOverlay); 
     mMapView.invalidate(); 
    } 

    //We can't use an anonymous class anymore if we want to change the position 
    // of the overlays/icons when location changed, give it a name 
    class Glistener implements OnItemGestureListener<OverlayItem> { 
     @Override 
     public boolean onItemLongPress(int index, OverlayItem item) { 
      Toast.makeText(SampleWithMinimapItemizedoverlay.this, "Item " + item.mTitle, 
        Toast.LENGTH_LONG).show(); 

      return false; 
     } 

     @Override 
     public boolean onItemSingleTapUp(int index, OverlayItem item) { 
      Toast.makeText(SampleWithMinimapItemizedoverlay.this, "Item " + item.mTitle, 
        Toast.LENGTH_LONG).show(); 
      return true; // We 'handled' this event. 

     } 

    } 
    // -------//here I tried to do the change !!!!!!!!!--------------- 
    // -- The map location being updated but not the overlayItem. 
    public void onLocationChanged(Location location) { 
     mLatitude = (int) (location.getLatitude() * 1E6); 
     mLongtitude = (int) (location.getLongitude() * 1E6); 
     Toast.makeText(SampleWithMinimapItemizedoverlay.this, 
       "Location changed. Lat:" + mLatitude + " long:" + mLongtitude, 
       Toast.LENGTH_LONG).show(); 
     GeoPoint gpt = new GeoPoint(mLatitude, mLongtitude); 
     mapController.setCenter(gpt); 
     mItems.clear(); // COMMENT OUT THIS LINE IF YOU WANT A NEW ICON FOR EACH CHANGE OF POSITION 
     mItems.add(new OverlayItem("New", "SampleDescription", gpt)); 
     // Change the overlay 
     this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(mItems, 
       new Glistener() , mResourceProxy); 
     this.mMapView.getOverlays().clear(); 
     this.mMapView.getOverlays().add(this.mMyLocationOverlay); 
     mMapView.invalidate(); 
    } 

    @Override 
    public void onProviderDisabled(String arg0) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 

} 

只是評論/取消註釋mItems.clear();取決於每次更改位置時是否需要新標記。 (該圖標出現在您已移動到的位置)

+0

我知道我不得不修改onLocationChanged方法。但是Glistener的目的是什麼? – rayman

+0

其中的代碼已經作爲匿名類存在,它是一個沒有名字並且在一個步驟中定義和實例化的類。因爲我們現在想要實例化它兩次,所以我們需要將代碼移動到內部類代碼塊併爲其命名(Glistener),以便我們可以使用'new Glistener()'表達式引用它。 – NickT

+0

我知道什麼是內心階層。但爲什麼你需要使用Glistener來驗證新座標中的圖標位置?很抱歉對於這個誤會。 – rayman