Jun 19, 2011

Easy drawing current location and compass on the MapView, using MyLocationOverlay.

com.google.android.maps provide a usefull class MyLocationOverlay to draw current location (and also compass) on the Map.

MyLocationOverlay

To use MyLocationOverlay is simply:
  • add a object of MyLocationOverlay in your mapView Overlays
  • call enableMyLocation() and/or enableCompass() in onResume()
  • call disableMyLocation() and/or disableCompass() onPause()
  • add "android.permission.ACCESS_FINE_LOCATION" in AndroidManifest.xml


The main.xml and MyItemizedOverlay.java have no change, you can copy the code from previous post Using ItemizedOverlay to add marker on MapView.

AndroidMapViewActivity.java
package com.AndroidMapView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class AndroidMapViewActivity extends MapActivity {
 
 MyItemizedOverlay myItemizedOverlay = null;
 MyLocationOverlay myLocationOverlay = null;
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       MapView mapView = (MapView) findViewById(R.id.mapview);
       mapView.setBuiltInZoomControls(true);
      
       Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
       int markerWidth = marker.getIntrinsicWidth();
       int markerHeight = marker.getIntrinsicHeight();
       marker.setBounds(0, markerHeight, markerWidth, 0);
      
       myItemizedOverlay = new MyItemizedOverlay(marker);
       mapView.getOverlays().add(myItemizedOverlay);
      
       GeoPoint myPoint1 = new GeoPoint(0*1000000, 0*1000000);
       myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
       GeoPoint myPoint2 = new GeoPoint(50*1000000, 50*1000000);
       myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");
      
       myLocationOverlay = new MyLocationOverlay(this, mapView);
       mapView.getOverlays().add(myLocationOverlay);
       mapView.postInvalidate();

   }

 @Override
 protected boolean isLocationDisplayed() {
  // TODO Auto-generated method stub
  return false;
 }

 @Override
 protected boolean isRouteDisplayed() {
  // TODO Auto-generated method stub
  return false;
 }

 @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();
 } 
}


AndroidManifest.xml, "android.permission.INTERNET" and "android.permission.ACCESS_FINE_LOCATION" is needed.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.AndroidMapView"
     android:versionCode="1"
     android:versionName="1.0">
   <uses-sdk android:minSdkVersion="4" />
   <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="com.google.android.maps" />
       <activity android:name=".AndroidMapViewActivity"
                 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-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest>


related: Implement own Custom MapView to move the routine works inside MapView.

10 comments:

  1. Good to know. I was unaware of this class and was about to create my on implementation. Thanks for sharing!

    ReplyDelete
  2. Wow this is exactly what I'm looking for. Thanks for this neat piece of code!

    One thing though, how do I actually "animateTo()" to my current location?

    animateTo takes a GeoPoint as an argument, however nowhere in your code specifies the current location as a GeoPoint, or any variable for that matter.

    How do i go about implementing that?

    ReplyDelete
  3. ^ Scratch my earlier question. I found this solution from stackoverflow that answers the above:

    locationOverlay.runOnFirstFix(new Runnable() {
    @Override
    public void run() {
    // TODO Auto-generated method stub mapController.animateTo(locationOverlay.getMyLocation());
    }
    });

    ReplyDelete
  4. I try to run with your code but compass don't appear. ณ thing may be missing something. Who will give me some advice. :D

    ReplyDelete
    Replies
    1. Make sure you have add "android.permission.ACCESS_FINE_LOCATION". And enableMyLocation() and/or enableCompass() in onResume().

      Delete
  5. Hey can anyone please tell me how to fence a region in android.......

    ReplyDelete
  6. How can you hide the compass after enable it? I want to take the bearing of the compass but without displaying the canvas on the screen (because I will be displaying something else instead based on the bearing)

    I tried to do so without enabling the compass but it's not possible... any ideas?

    ReplyDelete
    Replies
    1. To disable the compass, simple call myLocationOverlay.disableCompass().

      The compass described here is for display on MapView only. If you want to get the bearing, read Create our Android Compass.

      Delete
    2. Hello,

      Thanks for your fast reply but if I do disableCompass I won't be able to use getOrientation() - Still i will check the post you recommend me, i think i addresses my problem!

      Delete

Infolinks In Text Ads