2 Replies - 519 Views - Last Post: 05 February 2012 - 02:23 PM

Topic Sponsor:

#1 java_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-November 11

Google Places Markers Not Being Displayed on Second Map

Posted 04 February 2012 - 08:51 AM

I have created 2 google maps on my page to display markers from the Google Places API. But when I load the page the markers load on the second map instead of the first.
I have tried to create 2 maps based on different geocodes but i decided to use 2 scripts to try to rectify the problem, but to no avail. I know this may be a simple problem with my code but any suggestions would be helpful.

Here is my code
<?php
$tabTitle = ' | Maps';
$pageIntroductionHeading = 'Maps';
$pageIntroductionContent = 'Maps';
$column1Heading = 'London';
$column2Heading = 'New York';
$column3Heading = 'Paris';


require_once 'header.php';
require_once 'navigationMenu.php';
?>

<?php require_once 'getWeather.php'; ?>
<?php require_once 'getExchangeRates.php'; ?>

<?php require_once 'pageIntroduction.php'; ?>


<div id="obsah" class="content box">
    <div class="in">
        <div class="shadow">
            <img src="./img/threeCities.jpg" alt="Three Cities Banner" title="Three Cities" class="thumb" />
        </div>
        <ul class="columns">
            <li class="col1">




                <h3><?php
if (isset($column1Heading)) {
    echo $column1Heading;
}
?></h3>
                <p>
                    </br>
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=true&libraries=places"></script>

    <script type="text/javascript">
      var map;
      var infowindow;

      function initialize() {
        var london = new google.maps.LatLng(51.5069999695, -0.142489999533);

        map = new google.maps.Map(document.getElementById('map'), {
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          center:london,
          zoom: 10
        });
      

        var request = {
          location: london,
          radius: 5000
        
        };
        
        infowindow = new google.maps.InfoWindow();
        var service = new google.maps.places.PlacesService(map);
        service.search(request, callback);
      }

      function callback(results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
          for (var i = 0; i < results.length; i++) {
            createMarker(results[i]);
          }
        }
      }

      function createMarker(place) {
        var placeLoc = place.geometry.location;
        var marker = new google.maps.Marker({
         animation: google.maps.Animation.DROP,
         map: map,
          position: place.geometry.location
        });

        google.maps.event.addListener(marker, 'click', function() {
       
          infowindow.setContent(place.name);
          infowindow.open(map, this);
        });
        
      }

      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
                </head>  
            <body>
                <div id="map" style="width: 290px; height: 300px;"></div>
                
                <div id="text"></div>   
            </body> 
            </br>  

            </p>
            </li>
            <li class="col2">
                <h3><?php
                    if (isset($column2Heading)) {
                        echo $column2Heading;
                    }
?></h3>
                <p>
                    

    <script type="text/javascript">
      var map1;
      var infowindow;

      function initialize() {
        var nyc = new google.maps.LatLng(40.79445,-74.01558);

        map1 = new google.maps.Map(document.getElementById('map1'), {
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          center:nyc,
          zoom: 10
        });
      

        var request = {
          location: nyc,
          radius: 5000
        
        };
        
        infowindow = new google.maps.InfoWindow();
        var service = new google.maps.places.PlacesService(map1);
        service.search(request, callback);
      }

      function callback(results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
          for (var i = 0; i < results.length; i++) {
            createMarker(results[i]);
          }
        }
      }

      function createMarker(place) {
        var placeLoc = place.geometry.location;
        var marker = new google.maps.Marker({
         animation: google.maps.Animation.DROP,
         map: map1,
          position: place.geometry.location
        });

        google.maps.event.addListener(marker1, 'click', function() {
       
          infowindow.setContent(place.name);
          infowindow.open(map1, this);
        });
        
      }

      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
                  
                    </br>  
                     <body>
                <div id="map1" style="width: 290px; height: 300px;"></div>
                
                <div id="text"></div>   
            </body> 
                </p>
            </li>
            <li class="col3">
                <h3><?php
                    if (isset($column3Heading)) {
                        echo $column3Heading;
                    }
?></h3>
                <p>
                    </br>
                    
     


                    </br>  
    
                </p>
            </li>
        </ul>
        <div class="clear"></div>
    
    </div>
    <?php require_once 'footer.php'; ?>






Is This A Good Question/Topic? 0
  • +

Replies To: Google Places Markers Not Being Displayed on Second Map

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3239
  • View blog
  • Posts: 10,644
  • Joined: 18-April 07

Re: Google Places Markers Not Being Displayed on Second Map

Posted 05 February 2012 - 12:52 PM

Why does this page have two sets of <body> tags? You should always have just one body tag set in your page and within it you would have your two map div tags...

<body>
   <div id="map"></div>
   <div id="map1"></div>
</body>



Secondly don't try to replicate all the functions for each one and instead merge the two. By simply repeating the code with a subtle change you may actually be overwriting something in the first one. Try taking out the second script and see if you can get the first one working. Then try to add in the second map items into the first version's functions.

For instance, in the initialize method you can create two instances side by side...

function initialize() {
   var london = new google.maps.LatLng(51.5069999695, -0.142489999533);
   var nyc = new google.maps.LatLng(40.79445,-74.01558);

   map = new google.maps.Map(document.getElementById('map'), {
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          center:london,
          zoom: 10
   });

   map1 = new google.maps.Map(document.getElementById('map1'), {
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          center:nyc,
          zoom: 10
   });



See how I am doubling this up into one initialize? Since you are creating objects here, you can create new instances side by side like this. You can also edit your functions like createMarker to take in an extra parameter to be generic enough to work for both maps... that would give you only one function instead of two.

Having said that... now we move on to general errors.

1) You realize by doubling up the methods here you are defining TWO methods called "callback" and TWO called "createMarker". You can't do this. They have to be unique. Two functions can't be called the same.

2) In your second createMarker function you have "marker1" but you forgot to name the variable earlier in that function to "marker1" as well. You still call it "marker". In other words google.maps.event.addListener(marker1, 'click', function() { is using marker1, but you don't have marker1 defined and set. You forgot to change the name.

So correct this and try the suggestions above and you should be in better shape. :)

This post has been edited by Martyr2: 05 February 2012 - 12:53 PM

Was This Post Helpful? 2
  • +
  • -

#3 java_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-November 11

Re: Google Places Markers Not Being Displayed on Second Map

Posted 05 February 2012 - 02:23 PM

Thanks for your reply :)

I have now merged the two together in to one script. For it to display markers in all of the maps, am I right in thinking that i don't need to duplicate the callback and createmarker function but add code to the functions to display the markers?

Here is my code.

<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=true&libraries=places"></script>

    <script type="text/javascript">
      var map, map1, map2;
      var infowindow;

      function initialize() {
          var london = new google.maps.LatLng(51.5069999695, -0.142489999533);
          var nyc = new google.maps.LatLng(40.79445,-74.01558);
          var paris = new google.maps.LatLng(48.858001709, 2.29460000992);

        map = new google.maps.Map(document.getElementById('map'), {
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          center:london,
          zoom: 10
        });
      
         map1 = new google.maps.Map(document.getElementById('map1'), {
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          center:nyc,
          zoom: 10
        });
        
         map2 = new google.maps.Map(document.getElementById('map2'), {
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          center:paris,
          zoom: 10
        });

        var request = {
          location: london,
          radius: 5000
        
        };
        
        infowindow = new google.maps.InfoWindow();
        var service = new google.maps.places.PlacesService(map);
        service.search(request, callback);
       
      }

      function callback(results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
          for (var i = 0; i < results.length; i++) {
            createMarker(results[i]);
          }
        }
      }


      function createMarker(place) {
        var placeLoc = place.geometry.location;
        var marker = new google.maps.Marker({
         animation: google.maps.Animation.DROP,
         map: map,
      
          position: place.geometry.location
        });
       
 

Will the code here be...
var placeLoc1 = place.geometry.location;
        var marker1 = new google.maps.Marker({
         animation: google.maps.Animation.DROP,
         map: map1,
      
          position: place.geometry.location
        });


        google.maps.event.addListener(marker, 'click', function() {
       
          infowindow.setContent(place.name);
          infowindow.open(map, this);
        });
        
      }

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1