[ACCEPTED]-Adding Overlay to OSMDROID-osmdroid
The jars versions 3.0.3 and 3.0.4 seem to 10 have changed a bit since the sample code 9 was written.
If you are basing your app on 8 the code as quoted in the previous answer, then 7 change the line
mResourceProxy = new ResourceProxyImpl(getApplicationContext());
to
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
This should at least get 6 you going with a default icon.
EDIT
This should 5 centre the map just NE of Liverpool and 4 put an icon just off centre.
package osmdemo.demo;
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 DemoMap extends Activity implements LocationListener,
MapViewConstants {
private MapView mMapView;
private MapController mapController;
private LocationManager mLocMgr;
private ItemizedOverlay<OverlayItem> mMyLocationOverlay;
private ResourceProxy mResourceProxy;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
setContentView(R.layout.copymain);
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(53554070, -2959520); // centre map here
GeoPoint point3 = new GeoPoint(53554070 + 1000, -2959520 + 1000); // icon goes here
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(
DemoMap.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(
DemoMap.this,
"Item '" + item.mTitle ,Toast.LENGTH_LONG).show();
return false;
}
}, mResourceProxy);
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);
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) {}
}
It's a bit rough 3 and ready, but at least it runs OK with 2 just the 3.0.4 jar and the slf4j one. Hope 1 it helps.
EDIT 2
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="osmdemo.demo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="DemoMap"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
.
If you are trying to add an icon, then you 5 want to look at the ItemizedOverlay classes. You 4 can use osmdroid's ItemizedIconOverlay which 3 is a concrete implementation of ItemizedOverlay, or 2 you can subclass ItemizedOverlay on your 1 own. Take a look at the samples, such as:
@rayman
Please one more thing. When there 10 is a change in the location the map does 9 change but the Icon is gone. how do i validate 8 the Icon into the new change? – rayman 7 Jul 1 '11 at 7:12
this is where your location 6 changes
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint gpt = new GeoPoint(lat, lng);
mapController.setCenter(gpt);
mMapView.invalidate();
}
But as you can see it does not add 5 new icon to the overlay.
You should clear 4 the previous items
items.clear()
(if you want the "old" icon 3 to be removed when the new one is created) and 2 then add the new point (gpt) to the items.
items.add(new OverlayItem("Title", "Description", gpt));
and 1 so on...
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.