Reputation: 24194
I need to show my current location
as the Marker
always on the Google Map
using ItemizedOverlay
but whenever I run the below code, my marker doesn't showed up if I change the location from DDMS
. Below is my code. Is there anything wrong I am doing?
public class MainActivity extends MapActivity {
private MyItemizedOverlay myItemizedOverlay = null;
private LocationManager locationManager = null;
private MapView mapView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new GeoUpdateHandler());
}
public class GeoUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
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 point = new GeoPoint(lat, lng);
//GeoPoint myPoint1 = new GeoPoint((int) (37.347184*1000000), (int) (-121.966551*1000000));
myItemizedOverlay.addItem(point, "myPoint1", "myPoint1");
mapController.animateTo(point);
String address = ConvertPointToLocation(point);
Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
mapView.invalidate();
}
}
Below is MyItemizedOverlay class
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{
private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable marker) {
super(boundCenterBottom(marker));
// TODO Auto-generated constructor stub
populate();
}
public void addItem(GeoPoint p, String title, String snippet){
OverlayItem newItem = new OverlayItem(p, title, snippet);
overlayItemList.add(newItem);
populate();
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return overlayItemList.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return overlayItemList.size();
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
//boundCenterBottom(marker);
}
}
As I need to show other Marker's also on the Google Maps, so that is the reason I was going with ItemizedOverlay concept. Firstly I need to focus on my current location always using ItemizedOverlay
whenever my app starts. Any suggestions on my example will be appreciated.
Upvotes: 0
Views: 224
Reputation: 4114
GeoPoint point = new GeoPoint(lat, lng);
change above code
declare global variable
GeoPoint point;
then assign onLocationChanged() method
point = new GeoPoint(lat, lng);
Follow the very simple example link
http://android-coding.blogspot.com/2011/06/using-itemizedoverlay-to-add-marker-on.html
Upvotes: 0
Reputation: 82583
If all you want to do is display the user's current location, then use MyLocationOverlay. It is specifically designed for this. Here is a good example on how to use it, and below is the basic central code snippet:
// This goes into the onCreate method
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
}
});
See the tutorial for a more detailed example.
Upvotes: 1