When a user clicks on a position on the map, a dialog alert box opens up and prompts the user to whether or not he or she wants to add a marker, if yes the marker is added. I am trying to write another method that if the user passes this marker, the marker changes color.
I am having trouble writing the marker checking part of this.
My code:
public void onMapClick(){
map.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng latlng) {
dialogBox(latlng);
}
});
}
private void dialogBox(final LatLng latlng) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MapActivity.this);
alertDialogBuilder.setTitle("Add Marker");
alertDialogBuilder
.setMessage("Add marker?").setCancelable(false)
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
double dialogLat = latlng.latitude;
double dialogLng = latlng.longitude;
dialogMarker = map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
.position(latlng).title("dialog maker"));
//if location hit, places marker around that area
hitLocation(dialogLat,dialogLng);
}
})
.setNegativeButton("no", new DialogInterface.OnClickListener() {
//cancels
}
}); // create alert dialog show it
}
public void hitLocation( final double dialogLat, final double dialogLng){
LocationListener ll = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double lat = location.getLatitude();
double lon = location.getLongitude();
final LatLng currentLatLon = new LatLng(lat, lon);
//tracks weather or not person hit the location
if((lat > (dialogLat- 0.0001) && lat < (dialogLat+ 0.0001)) &&
(lon > (dialogLon - 0.0001) && lon < (dialogLon + 0.0001))){
dialogMarker = map.addMarker(new MarkerOptions().position(currentLatLon)
}
} // other location listener methods
It works up to where I pass hitLocation(doubleLat,doubleLng) inside the onClick method for the alert dialog. What I think should happen is that when I place a marker on the map, hitLocation should check whether or not I actaully hit the location that the user set.
It's really not clear how this all comes together (at least for me). For example, I don't see how hitLocation(double, double) is even called, but I do see a couple of issues.
First off. Is this a typo?
if((lat > (dialogLat- 0.0001) && lat < (dialogLng+ 0.0001))
did you mean
if((lat > (dialogLat- 0.0001) && lat < (dialogLat+ 0.0001))
dialog**Lat** vs dialog**Lng**
I think you would be better off checking if a coordinate is close by using something like this.
Math.abs(value1 - value2) < someTolerance
latitudes and longitudes are not always positive.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.