Jump to content
ICETuga

Mudar Marker Point ao aproximar de uma localização

Recommended Posts

ICETuga

Tenho uma localidade predefinida num mapa com um marker a vermelho assim que a actividade é criada. Defini também um LocationListener com a intenção para que quando estivermos perto desse local o Marker passe de Vermelho a Verde.

No entanto apesar de tudo funcionar o Marker simplesmente desaparece. Alguém me pode explicar o que estou a fazer mal?

A minha MainActivity que extende a MapActivity

private MapView mapa;
private MapController mapController;
private MyItemizedOverlay itemizedOverlay;
private List<Address> address;
private List<Overlay> mapOverlays;
Geocoder geocoder;
GeoPoint ponto;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mapa = (MapView) findViewById(R.id.themap);
mapa.setBuiltInZoomControls(true);
mapController = mapa.getController();
mapController.setZoom(14);
mapOverlays = mapa.getOverlays();
Drawable pin = getResources().getDrawable(R.drawable.map_pin_red);
Drawable pinHover = getResources().getDrawable(R.drawable.map_pin_green);
itemizedOverlay = new MyItemizedOverlay(MainActivity.this, pin);

ponto = new GeoPoint((int)(41.655738 * 1E6), (int)(-0.878112 * 1E6));
itemizedOverlay.addLocalization(41.655738, -0.878112, "Plaza del Pilar Zaragoza");
mapOverlays.add(itemizedOverlay);
mapController.animateTo(ponto);
mapController.setZoom(14);

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

MyLocationListener mListener = new MyLocationListener(this, mapController, itemizedOverlay, mapOverlays, pinHover);

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListener);
}

O listener

public class MyLocationListener implements LocationListener {

private Context context;
private MapController mapController;
private MyItemizedOverlay itemizedOverlay;
private List<Overlay> mapOverlays;
Drawable pin;

public MyLocationListener(Context context, MapController mapController,MyItemizedOverlay itemizedOverlay, List<Overlay> mapOverlays, Drawable pin) {
this.context = context;
this.mapController = mapController;
this.itemizedOverlay = itemizedOverlay;
this.mapOverlays = mapOverlays;
this.pin = pin;
}
@Override
public void onLocationchanged(Location location) {
GeoPoint ponto = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
mapController.animateTo(ponto);

if (verificaPertoLocal(location.getLatitude(), location.getLongitude())) {
itemizedOverlay.getItem(0).setMarker(pin);
mapOverlays.set(0, itemizedOverlay);
}
}

E o ItemizedOverlay

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context context;

public MyItemizedOverlay(Context context, Drawable defaultMarker) {
super(boundCenter(defaultMarker));
this.context = context;
}

public void addLocalization(double lat, double lon, String etiqueta) {
int lt = (int) (lat * 1E6);
int ln = (int) (lon * 1E6);

GeoPoint ponto = new GeoPoint(lt, ln);
OverlayItem item = new OverlayItem(ponto, etiqueta, null);
mOverlays.add(item);
populate();
}

public void clear() {
mOverlays.clear();
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}



@Override
public int size() {
return mOverlays.size();
}

@Override
protected boolean onTap(int index) {
String etiqueta = mOverlays.get(index).getTitle();
Toast.makeText(context, etiqueta, Toast.LENGTH_LONG).show();
return false;
}
}

Edited by ICETuga

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.