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.
0 Response to "Easy drawing current location and compass on the MapView, using MyLocationOverlay."
Posting Komentar