Search code examples
javascriptjquerygoogle-maps-api-3jquery-ui-map

Add single marker to map using google-ui-map plugin


This is example I'm trying to modify: http://jquery-ui-map.googlecode.com/svn/trunk/demos/jquery-google-maps-geocoding.html

I want to add single marker.

    var markers = $('#map_canvas').gmap('get', 'markers');

I get all markers, which are none in begining.

    if(markers == null) {

    $(map).click( function(event) {
        alert (event);
        $('#map_canvas').gmap('addMarker', {
            'position': event.latLng, 
            'draggable': true, 
            'bounds': false
        }, function(map, marker) {
            findLocation(marker.getPosition(), marker);
        }).dragend( function(event) {
            findLocation(event.latLng, this);
        });
    });
}

So if no marker allow to create one, but check again if there is one just enable to drag it.

My problem is that this if is not right. markers default gives empty string. Any idea?

--------

Solution

$(map).click( function(event) {

    if(!map.singleMarker) { 

    $('#map_canvas').gmap('addMarker', {
        'position': event.latLng, 
        'draggable': true, 
        'bounds': false
    }, function(map, marker) {
        findLocation(marker.getPosition(), marker);
    }).dragend( function(event) {
        findLocation(event.latLng, this);
    });

        map.singleMarker = true;        
    };  
});

Solution

  • Just to have as answered question:

    $(map).click( function(event) {
    
    if(!map.singleMarker) { 
    
    $('#map_canvas').gmap('addMarker', {
        'position': event.latLng, 
        'draggable': true, 
        'bounds': false
    }, function(map, marker) {
        findLocation(marker.getPosition(), marker);
    }).dragend( function(event) {
        findLocation(event.latLng, this);
    });
    
        map.singleMarker = true;        
    };  
    });