0

I'm trying to get the markers longitude and latitude and display them on the map but I'm not getting any result, my parser.php file is working and fetching the data from the database i just need to format it into javascript anyone ?

<script type="text/javascript">
  function initialize() {
    var mapOptions = {
      center: { lat: -25.363882, lng: 131.044922},
      zoom: 14
    };

    var map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);

    $.getJSON('parser.php', function(items) {
        for (var i = 0; i < items.length; i++) {
            (function(item) {
                addMarker(item.lat, item.lon);
            })(items[i]);
        }
    });

    }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

parser.php output

[{"0":"33.880561","lat":"33.880561","1":"35.542831","lon":"35.542831"},{"0":"-25.363882","lat":"131.044922","1":"35.513477","lon":"35.513477"}]
6
  • It would help to see a sample of the JSON object returned by parser.php. Commented Dec 14, 2014 at 3:26
  • Doesn't addMarker need to take the map as an argument? Commented Dec 14, 2014 at 3:27
  • @Barmar sorry first time i use $.getJSON I'm not sure if I'm using it the correct way, though google maps documentation shows the following adding a marker snippet, var myLatlng = new google.maps.LatLng(33.890537,35.513477); var marker = new google.maps.Marker({ position: myLatlng, map: map, title: 'Hello World!' }); Commented Dec 14, 2014 at 3:30
  • So put that code in the for loop. Commented Dec 14, 2014 at 3:32
  • What @Barmar said, or define an addMarker() function with the code you quoted. Commented Dec 14, 2014 at 3:34

1 Answer 1

1

addMarker needs to take map as an argument.

function addMarker(map, lat, long) {
    var latlong = google.maps.LatLng(lat, long);
    return new google.maps.Marker({
        position: latlong,
        map: map
    });
}

Then your loop should be:

for (var i = 0; i < items.length; i++) {
    item = items[i];
    addMarker(map, item.lat, item.lon);
}

You don't need to put the addMarker calls into a closure, because these aren't separate callbacks.

Sign up to request clarification or add additional context in comments.

6 Comments

can you tell me what I'm doing wrong cause i can't seem to make it work, function addMarker(map, lat, long) { var latlong = google.maps.LatLng(lat, long); return new google.maps.Marker({ position: latlong, map: map }); } $.getJSON('parser.php', function(items) { for (var i = 0; i < items.length; i++) { (function(item) { addMarker(map, item.lat, item.lon); })(items[i]); } });
You didn't put map in the arguments to addMarker.
Don't put long code blocks in comments, there's no formatting.
I've never used the Google Maps API, so I was just going by what you wrote. If that was correct, then my code should work.
Sorry, not interested in prolonging this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.