SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Placing google maps markers via an Array

    Hi Guys,
    I've got a few locations that I'd like to put on a google map and can do it just fine when I use the following code:

    Code:
                var map = new GMap2(document.getElementById("map_canvas"));
                map.setCenter(new GLatLng(52.583287, -0.237494), 13);
                var point1 = new GLatLng(52.583287,-0.237494)
                var point2 = new GLatLng(52.588345,-0.250711)
                map.addOverlay(new GMarker(point1));
                map.addOverlay(new GMarker(point2));
    However, when I try to do it via an array the markers appear, but not in the correct location and I can't figure out why.
    Below is my version where I attempt to use an array:

    Code:
        var x;
        var locations = new Array();
        locations[0] = "52.583287,-0.237494";
        locations[1] = "52.588345,-0.250711";
                for (x in locations) {
                    var point = new GLatLng(locations[x]);
                    map.addOverlay(new GMarker(point));
                }
    I'm not quite sure which part is wrong, but if anyone can point it out, I'd really appreciate it.

    Thanks

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not familiar with google maps but I think I spotted your error.

    You are setting the coordinates as a string and depending on how strict google maps is with strings and numeric values, it may be causing your script not to work.

    Original:

    new GLatLng(52.583287, -0.237494)

    Your script:
    var point = new GLatLng("52.583287,-0.237494");

    You can fix this by creating a bi-dimensional array. Each index holding the x and y values separately.

    Example:

    Code:
    var locations = [];
    locations[0] = [52.583287, -0.237494];
    Follow me @YaderHernandez

  3. #3
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried that but it doesn't work.
    But you are correct that I cannot use strings, after a little research I found out that it must be a pair of float numbers.

    In which case, my code should work fine when i remove the quotations, but it does not.

    Any other ideas?

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you show me your code after you removed the quotes?
    Follow me @YaderHernandez

  5. #5
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var x;
        var locations = new Array();
        locations[0] = [52.583287,-0.237494];
        locations[1] = [52.588345,-0.250711];
    
                for (x in locations) {
                    var point = new GLatLng(locations[x]);
                    map.addOverlay(new GMarker(point));
                }

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are accessing the index array correctly but it returns another array so it will never work.

    Code:
    var point = new GLatLng(locations[x][0], locations[x][1])
    That will allow you to access the correct indexes of the second array.
    Follow me @YaderHernandez

  7. #7
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    excellent, it works perfectly.
    I've been having a lot of trouble with that.

    Thank you very much for your help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •