Thursday, June 14, 2012

Add MyLocationOverlay on OpenStreetMap, to display user location and compass.

Modify the previous exercises "Implement OnItemGestureListener on OpenStreetMap to detect user touch and retrieve touched items properties" to add MyLocationOverlay on OpenStreetMap, to display user location and compass easily.

MyLocationOverlay on OpenStreetMap


Modify the main code, AndroidOpenStreetMapViewActivity.java. Add MyLocationOverlay in onCreate() method, enableMyLocation() and enableCompass() in onResume(), and disableMyLocation() and disableCompass() in onPause().

package com.exercise.OpenStreetMapView;

import java.util.ArrayList;
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.ItemizedIconOverlay.OnItemGestureListener;
import org.osmdroid.views.overlay.MyLocationOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import org.osmdroid.views.overlay.ScaleBarOverlay;

import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class AndroidOpenStreetMapViewActivity extends Activity {
 
 private MapView myOpenMapView;
 private MapController myMapController;
 
 ArrayList<OverlayItem> anotherOverlayItemArray;
 
 MyLocationOverlay myLocationOverlay = null;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myOpenMapView = (MapView)findViewById(R.id.openmapview);
        myOpenMapView.setBuiltInZoomControls(true);
        myMapController = myOpenMapView.getController();
        myMapController.setZoom(2);
      
        //--- Create Another Overlay for multi marker
        anotherOverlayItemArray = new ArrayList<OverlayItem>();
        anotherOverlayItemArray.add(new OverlayItem(
          "0, 0", "0, 0", new GeoPoint(0, 0)));
        anotherOverlayItemArray.add(new OverlayItem(
          "US", "US", new GeoPoint(38.883333, -77.016667)));
        anotherOverlayItemArray.add(new OverlayItem(
          "China", "China", new GeoPoint(39.916667, 116.383333)));
        anotherOverlayItemArray.add(new OverlayItem(
          "United Kingdom", "United Kingdom", new GeoPoint(51.5, -0.116667)));
        anotherOverlayItemArray.add(new OverlayItem(
          "Germany", "Germany", new GeoPoint(52.516667, 13.383333)));
        anotherOverlayItemArray.add(new OverlayItem(
          "Korea", "Korea", new GeoPoint(38.316667, 127.233333)));
        anotherOverlayItemArray.add(new OverlayItem(
          "India", "India", new GeoPoint(28.613333, 77.208333)));
        anotherOverlayItemArray.add(new OverlayItem(
          "Russia", "Russia", new GeoPoint(55.75, 37.616667)));
        anotherOverlayItemArray.add(new OverlayItem(
          "France", "France", new GeoPoint(48.856667, 2.350833)));
        anotherOverlayItemArray.add(new OverlayItem(
          "Canada", "Canada", new GeoPoint(45.4, -75.666667)));
        
        ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay 
         = new ItemizedIconOverlay<OverlayItem>(
           this, anotherOverlayItemArray, myOnItemGestureListener);
        myOpenMapView.getOverlays().add(anotherItemizedIconOverlay);
        //---
        
        //Add Scale Bar
        ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
        myOpenMapView.getOverlays().add(myScaleBarOverlay);
        
        //Add MyLocationOverlay
        myLocationOverlay = new MyLocationOverlay(this, myOpenMapView);
        myOpenMapView.getOverlays().add(myLocationOverlay);
        myOpenMapView.postInvalidate();
    }
    
    OnItemGestureListener<OverlayItem> myOnItemGestureListener
    = new OnItemGestureListener<OverlayItem>(){

  @Override
  public boolean onItemLongPress(int arg0, OverlayItem arg1) {
   // TODO Auto-generated method stub
   return false;
  }

  @Override
  public boolean onItemSingleTapUp(int index, OverlayItem item) {
   Toast.makeText(AndroidOpenStreetMapViewActivity.this, 
     item.mDescription + "\n"
     + item.mTitle + "\n"
     + item.mGeoPoint.getLatitudeE6() + " : " + item.mGeoPoint.getLongitudeE6(), 
     Toast.LENGTH_LONG).show();
   return true;
  }
     
    };
    
    @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  myLocationOverlay.enableMyLocation();
  myLocationOverlay.enableCompass();
 }

 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  myLocationOverlay.disableMyLocation();
  myLocationOverlay.disableCompass();
 }

}


"android.permission.ACCESS_FINE_LOCATION" is needed in AndroidManifest.xml. (already added in previous steps.)

Download the files.

2 comments:

Anonymous said...

Hi
Can you give me an example of how to use offline maps with this application you created.
tnx

Anonymous said...

im also waiting for offline osm map tutorial...thank you so much in advance ^_^