SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    0 Post(s)
    0 Thread(s)

    Values Disapearing from global arrays

    Hi Everyone,
    I'm trying to get multiple markers on a Google map using API V3. The markers I want to show are pink squares named beachflag.png. When my program gets to the setMarkers function the values inside the arrays created in the $.each loop are lost. The alert function displays undefined. I don't see how this is possible because I've declared this arrays at the beginning of the script (global). However, when the for loop at the bottom iterates through the location array it has only one value. All the values I pushed into the arrays(location, lat, and elong) are gone. I have been following an example from google sample api for v3( and it is not working for me. Here is my live test page

    var userLat="";
    var userLong="";
    var map;
    var eventsLat=[];
    var eventsLong=[];
    var eventmarkersArray=[];
    var i=0;
                function initiate_geolocation() {
                    //watch position
                function handle_errors(error)
                        case error.PERMISSION_DENIED: alert("user did not share geolocation data");
                        case error.POSITION_UNAVAILABLE: alert("could not detect current position");
                        case error.TIMEOUT: alert("retrieving position timed out");
                        default: alert("unknown error");
                function handle_geolocation_query(position){
                    var myOptions = {
    zoom: 8,
    center: new google.maps.LatLng(-34.397, 150.644),
    mapTypeId: google.maps.MapTypeId.ROADMAP
    map = new google.maps.Map(document.getElementById('map_canvas'),
    */                 var userLocation = new google.maps.LatLng(userLat, userLong);
                    var mapOptions = {
                              zoom: 8,
                             center: userLocation,
                             mapTypeId: google.maps.MapTypeId.ROADMAP
                     map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions);
                     var marker= new google.maps.Marker({
                    position: new google.maps.LatLng(userLat, userLong),
                    title: "Hello Testing",
                    clickable: true,
                    map: map
                          // here use $.get to send info to php page for server processing
                 var numRand = Math.floor(Math.random()*1000)
                        function (data){
                            var jsontext = data;
                            var contact = JSON.parse(jsontext);
                            $.each(contact , function() {
                                + this.event_name
                                + '</div><div>'
                                   + +"  "+this.elong
                                );// end div
                    });// end .each
    function setMarkers(map, locations) {
    alert (" inside function "+ eventsLat[i]);
    // Add markers to the map
    // Marker sizes are expressed as a Size of X,Y
    // where the origin of the image (0,0) is located
    // in the top left of the image.
    // Origins, anchor positions and coordinates of the marker
    // increase in the X direction to the right and in
    // the Y direction down.
    var image = new google.maps.MarkerImage('images/beachflag.png',
    // This marker is 20 pixels wide by 32 pixels tall.
    new google.maps.Size(20, 32),
    // The origin for this image is 0,0.
    new google.maps.Point(0,0),
    // The anchor for this image is the base of the flagpole at 0,32.
    new google.maps.Point(0, 32));
    var shadow = new google.maps.MarkerImage('images/beachflag.png',
    // The shadow image is larger in the horizontal dimension
    // while the position and offset are the same as for the main image.
    new google.maps.Size(37, 32),
    new google.maps.Point(0,0),
    new google.maps.Point(0, 32));
    // Shapes define the clickable region of the icon.
    // The type defines an HTML &lt;area&gt; element 'poly' which
    // traces out a polygon as a series of X,Y points. The final
    // coordinate closes the poly by connecting to the first
    // coordinate.
    var shape = {
    coord: [1, 1, 1, 20, 18, 20, 18 , 1],
    type: 'poly'
    for (var i = 0; i < locations.length; i++) {
    alert (" inside for loop "+ eventsLat[i]);
    var myLatLng = new google.maps.LatLng(eventLat[0], eventLong[0]);
    var marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    shadow: shadow,
    icon: image,
    shape: shape,
                }// end data callback
           &nbsp;);// end getJson
    Can you take a look over what I have done and let me know where I'm going wrong?
    Any programming suggestions would be helpful.
    Last edited by Force Flow; Mar 16, 2012 at 20:10. Reason: closed code tag

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Sydney, Australia
    11 Post(s)
    0 Thread(s)
    It looks like you've defined the global event lat / long vars like:
    var eventsLat = [];
    var eventsLong = [];
    (i.e. with a plural)

    Add when they're being used they are singular:

    var myLatLng = new google.maps.LatLng(eventLat[0], eventLong[0]);
    var details = {
    . . web: "",
    . . photos: "",
    . . psa: ""


Posting Permissions

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