0

I can't make MyLocationOverlay work, I searched for it and tried many codes but not a single working one. I only get a black screen. I need it to keep track of the user location. I managed to track the user location with regular overlay but I notice that for each new location the map is downloaded again (which is very anoing and not practical).

the code for using regular 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.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

public class OnlineTracking extends Activity implements LocationListener, MapViewConstants 
{
    private MapView mMapView; 
    private MapController mapController; 
    private LocationManager mLocMgr; 
    private ItemizedOverlay<OverlayItem> mMyLocationOverlay; 
    private ResourceProxy mResourceProxy; 

ArrayList<OverlayItem> items;

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext()); 
    setContentView(R.layout.main); 
    initilaizeMap();
    mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE); 
    mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 3, this);      
} 

public void initilaizeMap()
{
    mMapView = (MapView) this.findViewById(R.id.mapView); 
    mMapView.setTileSource(TileSourceFactory.MAPNIK); 
    //mMapView.setUseDataConnection(false);
    mMapView.setBuiltInZoomControls(true); 
    mMapView.setMultiTouchControls(true); 
    mapController = this.mMapView.getController(); 
    mapController.setZoom(15); 
    mapController.setCenter(new GeoPoint(15.610762,32.540345));  
}

public void displayLocation(GeoPoint loc)
{
    //mapController.setCenter(loc);
    items = new ArrayList<OverlayItem>(); 
    // Put overlay icon a little way from map center
    items.add(new OverlayItem("Here you are", "we will keep track of you", loc));
    /* 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( 
                            OnlineTracking.this, 
                            "single tap" + item.mTitle, Toast.LENGTH_LONG).show(); 
                    return true; // We 'handled' this event. 
                } 
                @Override 
                public boolean onItemLongPress(final int index, 
                        final OverlayItem item) { 
                    Toast.makeText( 
                            OnlineTracking.this,  
                            "longPress '" + item.mTitle ,Toast.LENGTH_LONG).show(); 
                    return false; 
                } 
            }, mResourceProxy); 
    mMapView.getOverlays().clear();
    this.mMapView.getOverlays().add(this.mMyLocationOverlay); 
    //mMapView.invalidate();
}

public void onLocationChanged(Location location)
{ 
    int lat = (int) (location.getLatitude() * 1E6); 
    int lng = (int) (location.getLongitude() * 1E6); 
    GeoPoint gpt = new GeoPoint(lat, lng); 
    displayLocation(gpt);
} 

@Override 
public void onProviderDisabled(String arg0) {} 

@Override 
public void onProviderEnabled(String provider) {} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) {} 
}
SeyedPooya Soofbaf
  • 2,654
  • 2
  • 29
  • 31
Abdalwhab Bakheet
  • 1,133
  • 2
  • 11
  • 17
  • try using the same mapview thing..and don't initialize new mapview...In OnLocationChange listener also use the same mapview..so that you can avoid new loading..can put some code here – Chet Jun 13 '12 at 10:15
  • Thanks chet for response, I don't initialize a new mapView I will post my code above – Abdalwhab Bakheet Jun 13 '12 at 17:20

1 Answers1

0

Seems that it is not a developer error. I have tried running the following sample project that uses osmdroid:

http://android-er.blogspot.it/2012/06/add-mylocationoverlay-on-openstreetmap.html

On an emulator running Android 2.2 I am getting the black screen at the early moment that MyLocationOverlay is instantiated. I tried to run the same project under an emulated device with the same specifications, but running Android 4.2, and it worked.

I really cannot explain why is this happenning, in my case i have chooosen to use the traditional approach and typical ItemizedIconOverlay

Andres Felipe
  • 625
  • 7
  • 24