Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google maps - pass information into an event Listener

I think this is a scope problem. Since the event is triggered after I have added all the listeners num_markers has always been overridden by the next cycle in the loop.

Is there some way I can pass variables to the event function?

I tried this approach but it didn't for me. Google Maps: Event Listener only remembering final value of variable

            var map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);
            var info_window = new google.maps.InfoWindow();
            var markers = [];
function load_markers() {
                var bounds_url = map.getBounds().toUrlValue();
                $.ajax({
                    url:'/retailer-markers?bounds='+bounds_url,
                    dataType: 'json',
                    success: function(data) {
                        for(i = 0; i < data.length; i++) {
                            var marker_pos = new google.maps.LatLng(data[i]['lat'], data[i]['long']);
                            //Every time the listener event is called this number is the length of the array
                            var marker_num = get_markers_count();

                            markers[marker_num] = new google.maps.Marker({
                                position: marker_pos,
                                map: map,
                                title:data[i]['Title'],
                                icon: image
                            });

                            google.maps.event.addListener(markers[marker_num], 'click', function() {
                                info_window.setContent('hello');
                                var pos = markers[marker_num].getPosition();
                                info_window.setPosition(pos);
                                info_window.open(map, markers[marker_num]);
                            });


                        }
                    }
                });
            }
like image 449
Keyo Avatar asked Sep 30 '10 03:09

Keyo


People also ask

How do I capture data from Google Maps?

To download your data, head to Google Maps on your computer and sign in. Next, click the three-line menu icon in the top-left corner next to the Search box. Near the bottom, select “Your Data in Maps.” On the next screen, scroll down to and select “Download Your Maps Data.”

How do I add an event to Google Maps?

To add an event, head to Google Maps on Android, tap on Contribute >Events > Add a public event. You can add an event name, tag the location, and add the time and date of the event as well. There's an option to add an image, write more event details, and add description as well.

What type of information you can get from Google Map?

In fact, it provides maps for almost every country in the world. Combined with satellite imagery, user-submitted photos, and Google's powerful street-view feature, Google Maps is more than just a map. It's an interactive atlas—a way to learn about different places and people all over the world.


1 Answers

The solution was to use this for the marker details. Any other variables can be set onto the marker with marker.set('some_var', data);

$.ajax({
                        url:'/retailer-markers?bounds='+bounds_url,
                        dataType: 'json',
                        success: function(data) {
                            for(i = 0; i < data.length; i++) {
                                var info_window = get_info_window();

                                var marker_pos = new google.maps.LatLng(data[i]['lat'], data[i]['long']);

                                marker_num = get_markers_count();

                                marker = new google.maps.Marker({
                                    position: marker_pos,
                                    map: map,
                                    title:data[i]['Title'],
                                    icon: image
                                });

                                markers.push(marker);
                                marker.set('retailer', data[i]);

                                google.maps.event.addListener(marker, 'click', function() {
                                    var retailer = this.get('retailer');

                                    info_window.setContent(retailer['name']);
                                    info_window.open(map, this);
                                });


                            }
                        }
                    });
like image 78
Keyo Avatar answered Oct 22 '22 00:10

Keyo