在同一页面上添加2个不同坐标的谷歌地图

时间:2013-03-22 12:08:34

标签: html google-maps-api-3 maps wordpress-theming

我一直在使用wordpress主题,该主题支持在菜单中添加坐标并直接在联系页面上显示地图。现在我有一个新的商家地址需要添加到联系人地图中,但主题不支持。

鉴于我在php和js方面的经验有限,我无法找到解决问题的方法。

我的联系页面代码如下:我需要的是在联系表单之前添加一个带有不同坐标的新地图。

<div id="content">
    <?php
    if ( have_posts() ) :
        while ( have_posts() ) : 
        the_post();
        $show_map = get_option('theme_gmap_show');
        if($show_map == 'true')
        {
        $map_lati = get_option('theme_gmap_lati');
        $map_longi = get_option('theme_gmap_longi');
        $map_zoom = get_option('theme_gmap_zoom');
    ?>                                                   
    <div class="map-container clearfix">
            <div id="map_canvas"></div>                         
            <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
            <script type="text/javascript">
            function initialize() 
            {
            var geocoder  = new google.maps.Geocoder();
            var map;
            var latlng = new google.maps.LatLng(<?php echo $map_lati; ?>, <?php echo $map_longi; ?>);
            var infowindow = new google.maps.InfoWindow();
            var myOptions = { 
                zoom: <?php echo $map_zoom; ?>,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            geocoder.geocode( { 'location': latlng }, 
              function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) 
                {
                  map.setCenter(results[0].geometry.location);
                  var marker = new google.maps.Marker({
                       map: map, 
                       position: results[0].geometry.location
                  });                                                                                                
                } 
                else 
                {
                  alert("Geocode was not successful for the following reason: " + status);
                }
             });
           }
           initialize();
           </script>        
          </div>                                                                                                                                
    <?php                                                   
    } 
    the_content(); 
            $postal_address = stripslashes(get_option('theme_postal_address'));
    if(!empty($postal_address))
    {
    ?>                                                                                                                                                                  
            <h3 class="smart-head">Adresa de contact</h3>
    <p><address>
   <?php echo $postal_address; ?>
</address></p>
<?php
}
?>
     <div class="contact-orar">
     <h3 class="smart-head">Program de Lucru</h3>
     <p><strong>Luni - Vineri:</strong> 8:00 - 17:00<br />
     <strong>Sambata:</strong> 8:00 - 14:00<br />
     <strong>Duminica:</strong> inchis</p>
     </div>
     <div class="contact-form-container">

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

即使没有任何PHP知识,您也应该能够管理添加地图,但代码使用的是Google Maps API v3。您可以使用JavaScript轻松添加其他地图。首先添加另一个标识为map_canvas_2的div,然后您应该使用以下Javascript代码:

<script>
  var map;

  function initialize() {
    var mapOptions = {
      zoom: 6,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: new google.maps.LatLng(/*latitude here*/,/*longitude here*/)
    };
    map = new google.maps.Map(document.getElementById('map_canvas_2'),
        mapOptions);
  }

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

这将显示以您在map_canvas_2 div中指定的坐标为中心的地图。您可以在此处找到更多信息:https://google-developers.appspot.com/maps/documentation/javascript/reference

JsFiddle:http://jsfiddle.net/Chycx/2/

答案 1 :(得分:0)

您必须在查询调用结束时添加随机密钥。这是使用KML图层的示例,但它是相同的概念。

$( function() {

   $(".map_canvas").each( function() {

           var theElement = $(this).attr('id');
           var lat = $(this).attr('lat');
           var lon = $(this).attr('lon');
           var kml = $(this).attr('kml');

           var mapCenter = new google.maps.LatLng(lat,lon);
           var mapOptions = {
             zoom: 14,
             center: mapCenter,
             mapTypeId: google.maps.MapTypeId.ROADMAP
           }

           var map = new google.maps.Map(document.getElementById(theElement), mapOptions);
           var geopoints = new google.maps.KmlLayer('http://blah.com/temps/kmls/' + kml + '.kml?run=' + (new Date()).getTime() ); //?nocache=0
           geopoints.setMap(map);
   });

})