Search code examples
javascriptgoogle-mapsdata-transfer

How pass data from parent page to popup?


I have following code:

parent page:

<!DOCTYPE html>

    <html>
      <head>
      </head>
      <body>
      <a href=# onclick='showMap()'>show map <a>
        <div id="map-canvas"></div>
      </body>
       <script>
       function showMap(){

         window.open('map.html','map','width=600,height=400');
        }
         </script>
    </html>

map.html:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Info windows</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>


function initialize() {
  var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var mapOptions = {
    zoom: 4,
    center: myLatlng
  };

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


  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Uluru (Ayers Rock)'
  });

}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

As you can see I use hardcode values for map marker rendering:

var myLatlng = new google.maps.LatLng(-25.363882,131.044922);

Can I pass these values from parent page to popup?


Solution

  • Since you asked just how to pass from parent to pop up an option would be querystring parameters:

    <a href=# onclick='showMap(-25.363882,131.044922)'>show map <a>
    
    function showMap(lat,lng){
     window.open('map.html?lat='+lat+'&lng ='+lng,'map','width=600,height=400');
    }
    

    Then in map.html collect those values and use..

    Update: Collecting query string values can be done in a lot of ways see the options with a quick search

    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }
           var lat = getParameterByName('lat');
           var lon = getParameterByName('lon');
    

    taken from : https://stackoverflow.com/a/901144/306921

    You can also take a look at: Get query string parameters with jQuery

    I tested and its all working:

    resulting index.html:

    <!DOCTYPE html>
    <html>
      <head>
      </head>
      <body>
      <a href=# onclick='showMap(-25.363882,131.044922)'>show map <a>
        <div id="map-canvas"></div>
      </body>
      <script>
       function showMap(lat,lng){
         window.open('map.html?lat='+lat+'&lng ='+lng,'map','width=600,height=400');
        }
      </script>
    </html>
    

    and the resulting map.html:

    <!DOCTYPE html>
    <html>
      <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <title>Info windows</title>
        <style>
          html, body, #map-canvas {
            height: 100%;
            margin: 0px;
            padding: 0px
          }
        </style>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
        <script>
        function getParameterByName(name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                results = regex.exec(location.search);
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
               var lat = getParameterByName('lat');
               var lon = getParameterByName('lon');
    
        function initialize() {
          var myLatlng = new google.maps.LatLng(lat, lon);
          var mapOptions = {
            zoom: 4,
            center: myLatlng
          };
    
      var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    
    
      var marker = new google.maps.Marker({
          position: myLatlng,
          map: map,
          title: 'Uluru (Ayers Rock)'
      });
    
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
        </script>
      </head>
      <body>
        <div id="map-canvas"></div>
      </body>
    </html>