在Google地图版本3上查找作者

时间:2012-06-08 作者:cmsdeployed

<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; ?>
我很难从经度和纬度转换到地址。我将感谢任何在正确方向上的帮助。

1 个回复
SO网友:Amit

如果您想在街道名称/编号中显示实际地址,则应使用反向地理编码apihttps://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding 基本上是这样的http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false 您可能应该在发布/保存帖子时(而不是在每次加载页面时)使用它。

如果您只是想在地图上显示标记,我几年前做了类似的事情,使用了一些代码,看起来与您的非常相似(自定义标记/信息痛苦),我的位置数组如下所示:

<?php
if ( $places_query->have_posts() ) {
    while ( $places_query->have_posts() ) {
        $places_query->the_post();
        $user_location[\'lat\'] = get_post_meta( $post->ID, \'geo_latitude\', true );
        $user_location[\'lng\'] = get_post_meta( $post->ID, \'geo_longitude\', true );
        $places[] = $user_location;
    }
}
?>
和实际阵列:

<script type="text/javascript">
    places =  <?php echo json_encode($places); ?>; //places is your var locations
</script>
不同的是我json_encode 发送到“贴图渲染”函数的数组。

希望有帮助!

结束

相关推荐

Users: List A to Z, for Users

我想创建一个代表我网站中每个用户的所有字母列表。示例:A -> Link that shows list of all users whose **nickname** starting with letter \"A\" B -> ...starting with letter \"B\" C -> ...starting with letter \"C\" D -> ...starting with letter \"D\" E -> ..