0

我有这些代码使用经度和纬度来定位谷歌地图上的帖子:进入标题

<script type="text/javascript">
var infowindow = new google.maps.InfoWindow();
var pinkmarker = new google.maps.MarkerImage('/wp-content/themes/mapdemo/pink_Marker.png', new google.maps.Size(20, 34) );
var shadow = new google.maps.MarkerImage('/wp-content/themes/mapdemo/shadow.png', new google.maps.Size(37, 34) );

    function initialize() {
    map = new google.maps.Map(document.getElementById('map'), { 
        zoom: 6, 
        center: new google.maps.LatLng(37.5407246, -77.4360481), 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
    });

    for (var i = 0; i < locations.length; i++) {  
        var marker = new google.maps.Marker({
            position: locations[i].geometry.location,
            icon: pinkmarker,
            shadow: shadow,
            map: map
        });
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
          return function() {
            infowindow.setContent(locations[i].info);
            infowindow.open(map, marker);
          }
        })(marker, i));
    }

     }
     </script>

在身体里

<?php if ( have_posts() ) : ?>
<!-- WordPress has found matching posts -->
<div style="display: none;">
  <?php $i = 1; ?>
  <?php while ( have_posts() ) : the_post(); ?>
  <?php if ( get_post_meta($post->ID, 'latlng', true) !== '' ) : ?>
  <div id="item<?php echo $i; ?>">
    <p><a href="<?php the_permalink(); ?>">
      <?php the_title(); ?>
      </a></p>
    <?php the_content(); ?>
  </div>
  <?php endif; ?>
  <?php $i++;   ?>
  <?php endwhile; ?>
</div>
<script type="text/javascript">
                var locations = [
                    <?php  $i = 1; while ( have_posts() ) : the_post(); ?>
                        <?php if ( get_post_meta($post->ID, 'latlng', true) !== '' ) : ?>
                            {
                                latlng : new google.maps.LatLng<?php echo get_post_meta($post->ID, 'latlng', true); ?>, 
                                info : document.getElementById('item<?php echo $i; ?>')
                        },
                        <?php endif; ?>
                    <?php $i++; endwhile; ?>
                ];
            </script>
<div id="map" style="width: 100%; height: 700px;"></div>
<?php else : ?>
<!-- No matching posts, show an error -->
<h1>Error 404 &mdash; Page not found.</h1>
<?php endif; ?>

我在从经度和纬度切换到地址时遇到了一些困难。我将不胜感激在正确方向上的任何帮助。

感谢http://www.billerickson.net,我最近一直在使用以下代码

if(is_single()):
global $post;
    $address = get_post_meta($post->ID,'address',true);
     if (!$address) $address = '123 Main St, Austin, TX';
    $google_api_key = get_option('maps_key');
    if($address): ?>
        <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=<?php echo $google_api_key; ?>" type="text/javascript"></script>
        <div id="map_canvas" style="width: 100%; height: 100px"></div>
        <script type="text/javascript">
            function showAddress(address) {
                var map = new GMap2(document.getElementById("map_canvas"));
                var geocoder = new GClientGeocoder();
                geocoder.getLatLng(
                    address,
                    function(point) {
                        if (!point) {
                            alert(address + " not found");
                        } else {
                            map.setCenter(point, 13);
                            var marker = new GMarker(point);
                            map.addOverlay(marker);
                        }
                    }
                );
            }
        showAddress("<?php echo $address; ?>");
    </script>
<?php endif;

万一;

在相应的作者页面上获取作者地址。我需要的是使用一个数组将所有作者放在同一张地图上,但我仍然很难让地图正常工作。

4

1 回答 1

0

使用 lat-lng 作为输入并检索地址的过程称为反向地理编码(正常地理编码将地址作为输入并返回 lat-lng)。Google 提供了一些地理编码选项:

  • Google Maps Developer's Guide的地理编码服务部分概述了以 JavaScript 客户端为中心的服务,该服务旨在用于响应网页上的动态用户交互的 JavaScript 代码。
  • Google Geocoding API Developer's Guide介绍了 API Web 服务,旨在更多地在服务器端用于预先知道的地址,而不是支持动态地图交互。

我已经包括了两者,因为看起来您正在使用静态数据。但我不确定您到底想要达到什么目标,因此地理编码服务可能更合适,或者将来会有用。请记住,这两个选项的服务条款都要求您使用在 Google 地图上下文中检索到的数据,并且不得保存数据来构建地理编码数据目录。还有使用限制和其他限制,因此您需要仔细阅读这些部分。我希望这是您正在寻找的,这会有所帮助-

于 2012-06-08T01:59:59.280 回答