Search code examples
androidoverlayosmdroid

How to call a method from a different class inside the main activity?


I am trying to place a marker on offline map which I have successfully displayed using osmdroid. To avoid cluttering my code. I want to define the method for overlay marker in a different class file. Any help would be highly appreciated.

My main activity goes like.

public class MainActivity extends Activity {
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MapView mapView = (MapView) findViewById(R.id.mapview);
    textView = (TextView) findViewById(R.id.tv_location);

    mapView.setTileSource(TileSourceFactory.MAPNIK);
    mapView.setBuiltInZoomControls(true);
    mapView.setMultiTouchControls(true);
    mapView.setClickable(true);

    MapController mapController = mapView.getController();
    mapController.setCenter(new GeoPoint(22.6772796,79.5897374));
    GeoPoint point1 = new GeoPoint(22.6772796,79.5897374);
    mapController.setZoom(10); 

    LocationManager myLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
    LocationListenerProxy llp=new LocationListenerProxy(myLocationManager);
    llp.startListening(gpsLocationListener, 1, 1) ;

    Marker mrkr = new Marker();
    mrkr.displayMarker();
}

My marker class is something like:

public class Marker {


public void displayMarker(){
    ResourceProxyImpl mResourceProxy = new ResourceProxyImpl(getApplicationContext());

    final RelativeLayout rl = new RelativeLayout(this);

    CloudmadeUtil.retrieveCloudmadeKey(getApplicationContext());

    this.mOsmv = new MapView(this, 256);
    rl.addView(this.mOsmv, new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));

}

}


Solution

  • There is no need to create a different class for this, you need to write a piece of code in you MainActiviy.java

    ArrayList<OverlayItem> anotherOverlayItemArray = new ArrayList<OverlayItem>();
        anotherOverlayItemArray.add(new OverlayItem("Bangalore","India", point1));
        OnItemGestureListener<OverlayItem> myOnItemGestureListener = null;
        ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay = new ItemizedIconOverlay<OverlayItem>(this, anotherOverlayItemArray, myOnItemGestureListener);
    
        map.getOverlays().clear();   
        map.getOverlays().add(anotherItemizedIconOverlay);  
        map.invalidate();
    

    use osmdroid-android-4.2.jar in your lib folder and do not forget to build Path. The jar file contains all the required images.