OverlayItem 未在位置更改时更新 (OSM)
我正在使用 OSMdroid。
感谢这篇文章 Adding Overlay to OSMDROID to createa override static item on a certain location 。
现在我正在尝试让地图更加动态一些。因此,当位置更改时,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)
{
}
}
谢谢,
雷。
I am using OSMdroid.
I have managed thanks to this post Adding Overlay to OSMDROID to creata overlay static item on a specific location.
now I am trying to make the map little bit more dynamic. so when the location change the OverLayItem (which marking the specific location on the map) will update itself also.
this is what I tried to do. the map does update but the OverLayItem doesnt.
code:
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)
{
}
}
Thanks,
ray.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以正常工作
。
只需注释/取消注释 mItems.clear();行取决于您每次更改位置时是否需要新标记。 (图标出现在您移动到的位置)
This should work OK
.
Just comment/uncomment the mItems.clear(); line depending on whether you want a new marker each time you change position. (The icon appears atthe position that you have moved to)