MJVDM
MJVDM

Reputation: 3963

find marker with jQuery.ui.maps -

I'm using jQuery.ui.maps to addMarkers and (hopefully) find them.

I added some markers as usual (which works including the icon), but with a tag and a value.

map.gmap('addMarker', { 'icon':'themes/images/cafetaria.png',                                                            
'tag':'Mensa', 'position': position }).click(clickcb);

later, I try to find all markers with that tag & value and hide them, but it never finds any.

map.gmap('find', 'markers', { 'property': 'tag', 'value': 'Mensa' }, function(marker, found) {
        if(found){marker.setVisible(false);}
    });

I cannot see the problem, as I basically copied code from the devs hp. Help is appreciated! THANKS

Upvotes: 1

Views: 6750

Answers (3)

Dobromir Minchev
Dobromir Minchev

Reputation: 367

It seems that this is an issue with the 3.0rc. In order to fix it I found a workaround - you have to define your marker property as array and to pass array with value(s) to the find method:

$('#map_canvas').gmap({'center': '42.2303, 24.7890' }).bind('init', function () {
$('#map_canvas').gmap('addMarker', { 'foo': ['bar'], 'position': '42.2303, 24.7890' });
$('#map_canvas').gmap('find', 'markers', { 'property': 'foo', 'value': ['bar'] }, function(marker, found) {
    marker.setVisible(found);
});

});

Upvotes: 1

Apostolos Emmanouilidis
Apostolos Emmanouilidis

Reputation: 7197

I have found a workaround. I'm setting the id and the title on the marker elements.

$('#map_canvas').gmap('addMarker', {id: i, 'position': new google.maps.LatLng(cityList[i][1], cityList[i][2]), title: cityList[i][0]});

Selection based on title or id:

$('#map_canvas').gmap('find', 'markers', { }, function(marker) {
    if(marker.title == markerName){marker.setVisible(false);}
});

OR

$('#map_canvas').gmap('find', 'markers', { }, function(marker) {
    if(marker.id == markerId){marker.setVisible(false);}
});

Below you can find a working example:

<!doctype html>
<html lang="en">
   <head>
        <title>jQuery mobile with Google maps - Google maps jQuery plugin</title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"> </script>
        <script type="text/javascript" src="http://jquery-ui-map.googlecode.com/svn/trunk/ui/min/jquery.ui.map.min.js"></script>
        <script type="text/javascript">

            var chicago = new google.maps.LatLng(41.850033,-87.6500523);
            var mobileDemo = { 'center': '41,-87', 'zoom': 7 };

            function initialize()
            {
                $('#map_canvas').gmap({ 'center': mobileDemo.center, 'zoom': mobileDemo.zoom, 'disableDefaultUI':false });
            }

            var cityList = [
                ['Chicago', 41.850033, -87.6500523, 1],
                ['Illinois', 40.797177,-89.406738, 2]
            ];

            function addMarkers()
            {
                for (var i = 0; i < cityList.length; i++) 
                {
                    var $marker = $('#map_canvas').gmap('addMarker', {id: i, 'position': new google.maps.LatLng(cityList[i][1], cityList[i][2]), title: cityList[i][0]});
                    $marker.click(function() {
                        $('#map_canvas').gmap('openInfoWindow', {'content': cityList[this.id][0]}, this);
                    });
                }
            }

            function hideMarkerByTitle(markerName)
            {
                $('#map_canvas').gmap('find', 'markers', { }, function(marker) {
                    if(marker.title == markerName){marker.setVisible(false);}
                });         
            }

            function hideMarkerById(markerId)
            {
                $('#map_canvas').gmap('find', 'markers', { }, function(marker) {
                    if(marker.id == markerId){marker.setVisible(false);}
                });         
            }

             $(document).on("pageinit", "#basic-map", function() {
                initialize();
            });

            $(document).on('click', '.add-markers', function(e) {
                e.preventDefault();
                addMarkers();
            });

            $(document).on('click', '.hide-chicago', function(e) {
                e.preventDefault();
                hideMarkerByTitle("Chicago");
                // uncomment to try by id
                //hideMarkerById(0);
            });

        </script>
    </head>
    <body>
        <div id="basic-map" data-role="page">
            <div data-role="header">
                <h1><a data-ajax="false" href="/">jQuery mobile with Google maps v3</a> examples</h1>
                <a data-rel="back">Back</a>
            </div>
            <div data-role="content">   
                <div class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;">
                    <div id="map_canvas" style="height:350px;"></div>
                </div>
                <a href="#" class="add-markers" data-role="button" data-theme="b">Add Some More Markers</a>
                <a href="#" class="hide-chicago" data-role="button" data-theme="b">Hide Chicago</a>
            </div>
        </div>      
    </body>
</html>

I hope this helps.

Upvotes: 7

vhugo
vhugo

Reputation: 553

vMap is a lightning jQuery plugin with HTML 5 that makes Google Maps easy by sending a simple JSON Data Structure.

https://github.com/vhugogarcia/vMap

It helped me to solve a lot the markers problem with Google Maps, but also adds the feature for listing the locations if needed.

Upvotes: 0

Related Questions