Cdacians

Cdacians
Cdacians

Tuesday, 7 August 2012

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


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

com.google.android.maps provide a usefull classMyLocationOverlay 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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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();
 }
}


akm
www.cdacians.com

No comments:

Post a Comment