Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 9th, 2012  |  syntax: None  |  size: 8.53 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. OverlayItem not updating on location change (OSM)
  2. package com.test.overlay;
  3.  
  4. import java.util.ArrayList;
  5. import org.osmdroid.DefaultResourceProxyImpl;
  6. import org.osmdroid.ResourceProxy;
  7. import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
  8. import org.osmdroid.util.GeoPoint;
  9. import org.osmdroid.views.MapController;
  10. import org.osmdroid.views.MapView;
  11. import org.osmdroid.views.overlay.ItemizedIconOverlay;
  12. import org.osmdroid.views.overlay.ItemizedOverlay;
  13. import org.osmdroid.views.overlay.OverlayItem;
  14. import org.osmdroid.views.util.constants.MapViewConstants;
  15.  
  16. import android.app.Activity;
  17. import android.location.Location;
  18. import android.location.LocationListener;
  19. import android.location.LocationManager;
  20. import android.os.Bundle;
  21. import android.widget.Toast;
  22.  
  23. public class SampleWithMinimapItemizedoverlay extends Activity implements
  24.         LocationListener, MapViewConstants
  25. {
  26.  
  27.     private MapView mMapView;
  28.     private MapController mapController;
  29.     private LocationManager mLocMgr;
  30.     private ItemizedOverlay<OverlayItem> mMyLocationOverlay;
  31.     private ResourceProxy mResourceProxy;
  32.     int longtitude = 31987968;
  33.     int latitude = 34783155;
  34.  
  35.     @Override
  36.     public void onCreate(Bundle savedInstanceState)
  37.     {
  38.         super.onCreate(savedInstanceState);
  39.  
  40.         mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
  41.         setContentView(R.layout.main);
  42.  
  43.         mMapView = (MapView) this.findViewById(R.id.mapview);
  44.         mMapView.setTileSource(TileSourceFactory.MAPNIK);
  45.         mMapView.setBuiltInZoomControls(true);
  46.         mMapView.setMultiTouchControls(true);
  47.         mapController = this.mMapView.getController();
  48.         mapController.setZoom(15);
  49.         GeoPoint point2 = new GeoPoint(longtitude, latitude); // centre map here
  50.         GeoPoint point3 = new GeoPoint(longtitude + 2000, latitude + 2000); // icon
  51.                                                                             // goes
  52.                                                                             // here
  53.         // 31.987968,34.783155
  54.  
  55.         mapController.setCenter(point2);
  56.         mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
  57.         mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100,
  58.                 this);
  59.  
  60.         ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
  61.         // Put overlay icon a little way from map centre
  62.         items.add(new OverlayItem("Here", "SampleDescription", point3));
  63.  
  64.         /* OnTapListener for the Markers, shows a simple Toast. */
  65.         this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
  66.                 new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>()
  67.                 {
  68.                     @Override
  69.                     public boolean onItemSingleTapUp(final int index,
  70.                             final OverlayItem item)
  71.                     {
  72.                         Toast.makeText(SampleWithMinimapItemizedoverlay.this,
  73.                                 "Item '" + item.mTitle, Toast.LENGTH_LONG)
  74.                                 .show();
  75.                         return true; // We 'handled' this event.
  76.                     }
  77.  
  78.                     @Override
  79.                     public boolean onItemLongPress(final int index,
  80.                             final OverlayItem item)
  81.                     {
  82.                         Toast.makeText(SampleWithMinimapItemizedoverlay.this,
  83.                                 "Item '" + item.mTitle, Toast.LENGTH_LONG)
  84.                                 .show();
  85.                         return false;
  86.                     }
  87.                 }, mResourceProxy);
  88.         this.mMapView.getOverlays().add(this.mMyLocationOverlay);
  89.         mMapView.invalidate();
  90.     }
  91.  
  92.     -------//here I tried to do the change !!!!!!!!!---------------
  93.     -- The map location being updated but not the overlayItem.
  94.     public void onLocationChanged(Location location)
  95.     {
  96.         latitude = (int) (location.getLatitude() * 1E6);
  97.         longtitude = (int) (location.getLongitude() * 1E6);
  98.         Toast.makeText(SampleWithMinimapItemizedoverlay.this,
  99.                 "Location changed. Lat:" + latitude + " long:" + longtitude ,
  100.                 Toast.LENGTH_LONG).show();
  101.         GeoPoint gpt = new GeoPoint(latitude, longtitude);
  102.         mapController.setCenter(gpt);
  103.         mMapView.invalidate();
  104.     }
  105.  
  106.     @Override
  107.     public void onProviderDisabled(String arg0)
  108.     {
  109.     }
  110.  
  111.     @Override
  112.     public void onProviderEnabled(String provider)
  113.     {
  114.     }
  115.  
  116.     @Override
  117.     public void onStatusChanged(String provider, int status, Bundle extras)
  118.     {
  119.     }
  120.  
  121. }
  122.        
  123. public class SampleWithMinimapItemizedoverlay extends Activity implements
  124.         LocationListener, MapViewConstants {
  125.  
  126.     private MapView mMapView;
  127.     private MapController mapController;
  128.     private LocationManager mLocMgr;
  129.     private ItemizedOverlay<OverlayItem> mMyLocationOverlay;
  130.     private ResourceProxy mResourceProxy;
  131.     int mLongtitude = 31987968;
  132.     int mLatitude = 34783155;
  133.     ArrayList<OverlayItem> mItems;
  134.     @Override
  135.     public void onCreate(Bundle savedInstanceState) {
  136.         super.onCreate(savedInstanceState);
  137.  
  138.         mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
  139.         setContentView(R.layout.main);
  140.  
  141.         mMapView = (MapView) this.findViewById(R.id.mapview);
  142.         mMapView.setTileSource(TileSourceFactory.MAPNIK);
  143.         mMapView.setBuiltInZoomControls(true);
  144.         mMapView.setMultiTouchControls(true);
  145.         mapController = this.mMapView.getController();
  146.         mapController.setZoom(15);
  147.         GeoPoint point2 = new GeoPoint(mLongtitude, mLatitude); // centre map here
  148.         GeoPoint point3 = new GeoPoint(mLongtitude + 2000, mLatitude + 2000); // icon
  149.                                                                             // goes
  150.         // 31.987968,34.783155
  151.         mapController.setCenter(point2);
  152.         mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
  153.         mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100,
  154.                 this);
  155.  
  156.         mItems = new ArrayList<OverlayItem>();
  157.         // Put overlay icon a little way from map centre
  158.         mItems.add(new OverlayItem("Here", "SampleDescription", point3));
  159.  
  160.         /* OnTapListener for the Markers, shows a simple Toast. */
  161.         // REFER TO THE GESTURE LISTEMER BY NAME NOW
  162.         this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(mItems,
  163.                 new Glistener() , mResourceProxy);
  164.         this.mMapView.getOverlays().add(this.mMyLocationOverlay);
  165.         mMapView.invalidate();
  166.     }
  167.  
  168.     //We can't use an anonymous class anymore if we want to change the position
  169.     // of the overlays/icons when location changed, give it a name
  170.     class Glistener implements OnItemGestureListener<OverlayItem> {
  171.         @Override
  172.         public boolean onItemLongPress(int index, OverlayItem item) {
  173.             Toast.makeText(SampleWithMinimapItemizedoverlay.this, "Item " + item.mTitle,
  174.                     Toast.LENGTH_LONG).show();
  175.  
  176.             return false;
  177.         }
  178.  
  179.         @Override
  180.         public boolean onItemSingleTapUp(int index, OverlayItem item) {
  181.             Toast.makeText(SampleWithMinimapItemizedoverlay.this, "Item " + item.mTitle,
  182.                     Toast.LENGTH_LONG).show();
  183.             return true; // We 'handled' this event.
  184.  
  185.         }
  186.  
  187.     }
  188.     // -------//here I tried to do the change !!!!!!!!!---------------
  189.     // -- The map location being updated but not the overlayItem.
  190.     public void onLocationChanged(Location location) {
  191.         mLatitude = (int) (location.getLatitude() * 1E6);
  192.         mLongtitude = (int) (location.getLongitude() * 1E6);
  193.         Toast.makeText(SampleWithMinimapItemizedoverlay.this,
  194.                 "Location changed. Lat:" + mLatitude + " long:" + mLongtitude,
  195.                 Toast.LENGTH_LONG).show();
  196.         GeoPoint gpt = new GeoPoint(mLatitude, mLongtitude);
  197.         mapController.setCenter(gpt);
  198.         mItems.clear(); // COMMENT OUT THIS LINE IF YOU WANT A NEW ICON FOR EACH CHANGE OF POSITION
  199.         mItems.add(new OverlayItem("New", "SampleDescription", gpt));
  200.         // Change the overlay
  201.         this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(mItems,
  202.                 new Glistener() , mResourceProxy);
  203.         this.mMapView.getOverlays().clear();
  204.         this.mMapView.getOverlays().add(this.mMyLocationOverlay);
  205.         mMapView.invalidate();
  206.     }
  207.  
  208.     @Override
  209.     public void onProviderDisabled(String arg0) {
  210.     }
  211.  
  212.     @Override
  213.     public void onProviderEnabled(String provider) {
  214.     }
  215.  
  216.     @Override
  217.     public void onStatusChanged(String provider, int status, Bundle extras) {
  218.     }
  219.  
  220. }