Issue
I want to add an onClicklistener to a Button; so when I press it, it animatesTo user location. However, if there is no user location and the button is pressed, it crashes due to user location = null. How do I make it run ONLY when it is not equal to null?
I have tried -
private void myLocationButton() {
MapView mp = (MapView) findViewById(R.id.mapView);
final MapController mapController = mp.getController();
final Button myLocationButton = (Button) findViewById(R.id.myLocationButton);
myLocationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myLocationButton.isClickable()) {
mapController.animateTo(myLocationOverlay.getMyLocation());
mapController.setZoom(17);
}
}
});
}
This is a long post, thank you for your ample time.
Solution
try this:
@Override
public void onClick(View v) {
if (myLocationButton.isClickable() && myLocationOverlay.getMyLocation() != null) {
mapController.animateTo(myLocationOverlay.getMyLocation());
mapController.setZoom(17);
}
}
});
Answered By - ρяσѕρєя K
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.