how to get JSON Results placed on Google Maps?

蹲街弑〆低调 提交于 2019-12-06 07:00:54

In the core of your above the example code is looping 10 times adding random points - you need to replace that with a loop over your points.

Specifically this bit:

       // loop over your points
           for (var i = 0; i < 10; i++) {
                // use your points lat/lng
                var latlng = new google.maps.LatLng(Math.random() * 180 - 90, Math.random() * 360 - 180);
                bounds.extend(latlng);
                var marker = new google.maps.Marker({
                    position: latlng,
                    // animation: google.maps.Animation.DROP, // animation disabled because it slows down performance
                    // use your points name
                    title: "RedStar Creative Marker #" + i
                });
                mgr.addMarker(marker, 0);
            }

Here is an untested first go, no guarantees, blah blah blah :)

       // loop over your points
       for (var j in layer["users"]) {
                var place = layer["users"][j];
                // use your points lat/lng
                var latlng = new google.maps.LatLng(place["posn"][0], place["posn"][1]);
                bounds.extend(latlng);
                var marker = new google.maps.Marker({
                    position: latlng,
                    // animation: google.maps.Animation.DROP, // animation disabled because it slows down performance
                    // use your points name
                    title: place["name"]
                });
                mgr.addMarker(marker, 0);
            }

this is my JSON result i want to be able to get this image in to info window on google maps when user clicks on marker

image":"http://graph.facebook.com/10000090226****/picture?type=large"

here is part of the coding for the google maps

<script type="text/javascript">

    var map;
    var bounds = new google.maps.LatLngBounds();
    var mgr;

    function initialize() {
        var myOptions = {
            zoom: 2,
            center: new google.maps.LatLng(53.491314, -2.249451),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        mgr = new MarkerManager(map);
        google.maps.event.addListener(mgr, 'loaded', function () {
            $.ajax({
                url: "/Home/GetUser",
                context: document.body,
                success: function (data) {
                    for (var i = 0; i < data.user.length; i++) {
                        var label = data.user[i].name;
                        var lat = data.user[i].posn[0];
                        var long = data.user[i].posn[1];

                        var latlng = new google.maps.LatLng(lat, long);
                        bounds.extend(latlng);
                        var marker = new google.maps.Marker({
                            position: latlng,
                            title: label
                        });

                        mgr.addMarker(marker, 0);

                    }
                    //adding google maps window popup...
                    var infowindow = new google.maps.InfoWindow();
                    google.maps.event.addListener(marker, 'click', function () {
                        infowindow.open(map, marker);
                    });
                }

            });

            mgr.refresh();

            map.fitBounds(bounds);
        });
    }
    $(document).ready(function () {
        initialize();
    });

    </script>

at the moment its not opening a window, wont it even open if there is no content inside it? bit confused on trying to get the image inside the content window. I'll have to keep digging around and see if i can manage to get in the URL image in there.

does the adding window popup bit need to come after the refresh of the markers?

UPDATE; sorted the problem, was adding bits of coding in the wrong places, needed some tweaking. whats happening now is when you click the first marker it opens the info window on the second marker and displays the name, but the first marker that was initially clicked does not open an infoWindow.

function initialize() {
        var myOptions = {
            zoom: 10,
            center: new google.maps.LatLng(0, 0),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        mgr = new MarkerManager(map);
        google.maps.event.addListener(mgr, 'loaded', function () {

            $.ajax({
                url: "/Home/GetUser",
                context: document.body,
                success: function (data) {
                    for (var i = 0; i < data.user.length; i++) {
                        var label = data.user[i].name;
                        var lat = data.user[i].posn[0];
                        var long = data.user[i].posn[1];

                        var latlng = new google.maps.LatLng(lat, long);

                        bounds.extend(latlng);
                        map.fitBounds(bounds);

                        var marker = new google.maps.Marker({
                            position: latlng,
                            title: label
                        });

                        var infowindow = new google.maps.InfoWindow({ content: data.user[i].name });
                        google.maps.event.addListener(marker, 'click', function () {
                            infowindow.open(map, marker);
                        });

                        mgr.addMarker(marker, 1);

FINAL UPDATE: right i can stop panicking, managed to get it working after slapping myself to wake up and read things more than 3 times :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!