在 osmdroid 中使用 MylocationOverlay



我无法使MyLocationOverlay工作,我搜索了它并尝试了许多代码,但没有一个有效的代码。我只得到一个黑屏。我需要它来跟踪用户位置。我设法使用常规叠加跟踪用户位置,但我注意到对于每个新位置,地图都会再次下载(这非常不实用且不实用)。

使用常规覆盖的代码:

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) {} 
}
似乎

这不是开发人员错误。我尝试运行以下使用osmdroid的示例项目:

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

在运行Android 2.2的模拟器上,我在实例化MyLocationOverlay的早期出现黑屏。我尝试在具有相同规格的模拟设备下运行相同的项目,但运行Android 4.2,并且它起作用了。

真的无法解释为什么会发生这种情况,就我而言,我可以选择使用传统方法和典型ItemizedIconOverlay

相关内容

  • 没有找到相关文章

最新更新