Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google map api v3 - remove old marker when doing geocoding

I am new in Javascript and google map api and I have been follow this link to remove a marker but some how I cannot make it work.

Basically I want to use a button to generate the marker when user enter an address and click on the button. When the user enter a new address and click on the button again, the old marker will be removed and the new marker pin on the new address. The marker also draggable.

Here is my js code:

$('#geocode').live('click',function() {
        codeAddress();
        return false;
});    

function codeAddress() {
                    var address = document.getElementById('location').value;
                    geocoder.geocode( { 'address': address}, function(results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {

                                map.setCenter(results[0].geometry.location);
                                if (marker) marker.setMap(null);
                                if (marker) delete marker;
                                var marker = new google.maps.Marker({
                                     draggable:true,    
                                      map: map,
                                      position: results[0].geometry.location
                                  });

                                 var newlat = results[0].geometry.location.lat();
                                 var newlng = results[0].geometry.location.lng(); 
                                 document.getElementById('mwqsflatlng').value = (newlat+' , '+newlng);
                                  draggeablemarker(marker);
                                } else {
                                  alert('Geocode was not successful for the following reason: ' + status);
                                }
                    });
            }

Update When I check on the inspect element, it gave me this error:

Uncaught TypeError: Cannot call method 'setMap' of undefined

like image 678
ron_dev Avatar asked Dec 07 '22 07:12

ron_dev


1 Answers

You need to have a reference to your marker object to be able to access it at a later time. If you want to restrict the map to one marker showing at a time you can update the markers Position property instead of deleting and recreating it.

Here is a function that can change the markers position or create a new marker if one does not exist on the map. The location parameter is a Google LatLng object which is the same as the object returned by the Geocoder results[0].geometry.location.

Notice the marker variable is defined outside of the function scope. This is what allows you to refer to the marker at a later time.

var marker;

function placeMarker(location) {
    if (marker) {
        //if marker already was created change positon
        marker.setPosition(location);
    } else {
        //create a marker
        marker = new google.maps.Marker({
            position: location,
            map: map,
            draggable: true
        });
    }
}

So for your geocode success function you should only have to pass the result to this function.

geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
       placeMarker(results[0].geometry.location);

}
...

Here is a fiddle of of the concept. You can click on the map and the marker will move to the desired location.

like image 70
Bryan Weaver Avatar answered Jan 21 '23 13:01

Bryan Weaver