Issue
I have a map which at the moment I place a custom pin on the map every so often from the GPS position of the user, this is also dependent on when their position changes but with the GPS being a bit choppy here it does it quite a lot. At the moment a new pin is placed on the map every time this happens. I want to remove the previous pin so that just the one pin for the users location is shown on the map, like a live representation of the users position.
In addition to this. Is it possible to have a separate set of pins on the map (which their positions never change) and ensure they are not also cleared when the users position is updated.
Any other comments on the code would be appreciated.
Imports...
public class MapOne extends MapActivity implements LocationListener {
... fields
@Override
protected void onCreate(Bundle icicle) {
....onCreate initialisations
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
compass.disableCompass();
lm.removeUpdates(this);
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
compass.enableCompass();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 20, this);
super.onResume();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public void onLocationChanged(Location l) {
// TODO Auto-generated method stub
lat = (int)(l.getLatitude() *1E6);
longi = (int)(l.getLongitude() *1E6) ;
GeoPoint ourLocation = new GeoPoint(lat, longi);
OverlayItem overlayItem = new OverlayItem(ourLocation, "Hey!", "What's up!?");
CustomPinpoint custom = new CustomPinpoint(customMarker, Map.this);
custom.insertPinpoint(overlayItem);
overlayList.add(custom);
}
}
Pinpoint class for reference if required.
public class Pinpoint extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
private Context c;
public CustomPinpoint(Drawable defaultMarker) {
super(boundCenter(defaultMarker));
// TODO Auto-generated constructor stub
}
public CustomPinpoint(Drawable m, Context context) {
// TODO Auto-generated constructor stub
this(m);
setC(context);
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return pinpoints.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return pinpoints.size();
}
public void insertPinpoint(OverlayItem item) {
pinpoints.add(item);
this.populate();
}
public Context getC() {
return c;
}
public void setC(Context c) {
this.c = c;
}
}
Solution
If you want to add a Pin that follows the users current location,you can use com.google.android.maps.MyLocationOverlay ....this provides you with the required listeners also.You can also override its onDraw() method to draw your custom pin.You can then add this overlay just like anyother overlay...don't forget to call myLocationOverlay.enableMyLocation() so that you recieve the location changes.
Answered By - Navin Ilavarasan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.