【问题标题】:Geocoding Google Maps in Wordpress not working在 Wordpress 中对 Google 地图进行地理编码不起作用
【发布时间】:2015-02-08 21:42:35
【问题描述】:

我想使用 Wordpress 帖子的标题(位置)成为 Google 地图上的可见标记。来自 Google 的这段代码可以很好地显示地图(没有标记):

<script>function initialize() {
    var mapCanvas = document.getElementById('map-canvas');
    var mapOptions = {
      center: new google.maps.LatLng(44.5403, -78.5463),
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(mapCanvas, mapOptions)
  }
  google.maps.event.addDomListener(window, 'load', initialize);</script>

但是,当我尝试实施地理编码部分时,地图没有显示。我尝试过的两种解决方案:

解决方案 1

    <script type="text/javascript">// <![CDATA[


var mapOptions = {
    zoom: 16,
    center: new google.maps.LatLng(54.00, -3.00),
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

var geocoder = new google.maps.Geocoder();

var address = '3410 Taft Blvd  Wichita Falls, TX 76308';

geocoder.geocode( { 'address': address}, 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);
    }
});

var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
// ]]></script>

2012 年 3 月 17 日,http://manofhustle.com/2012/03/17/google-map-geocoding/

解决方案 2:

    var geocoder;
    var map;
    var address = "San Diego, CA";

    function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
    zoom: 8,
    center: latlng,
    mapTypeControl: true,
    mapTypeControlOptions: {
      style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
    },
    navigationControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
    if (geocoder) {
    geocoder.geocode({
      'address': address
    }, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
          map.setCenter(results[0].geometry.location);

          var infowindow = new google.maps.InfoWindow({
            content: '<b>' + address + '</b>',
            size: new google.maps.Size(150, 50)
          });

          var marker = new google.maps.Marker({
            position: results[0].geometry.location,
            map: map,
            title: address
          });
          google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map, marker);
          });

        } else {
          alert("No results found");
        }
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }
}
google.maps.event.addDomListener(window, 'load', initialize);

2014 年 12 月 4 日,Using Address Instead Of Longitude And Latitude With Google Maps API

我做错了什么或没有看到?

编辑:在 getElementByID 中将地图更改为地图画布,但仍然不起作用。

编辑 2:通过 iframe 可以工作(但您不能在地图中移动,只能滚动)

<iframe width="195" height="195" frameborder="0" scrolling="yes" marginheight="0" marginwidth="0" src="https://maps.google.ca/maps?center=<?php the_title('Groningen'); ?>&q=<?php the_title('Groningen'); ?>&size=195x195&output=embed&iwloc=near"></iframe>enter code here

【问题讨论】:

    标签: javascript wordpress google-maps google-maps-api-3


    【解决方案1】:

    工作示例.........http://jsfiddle.net/vezkvkve/1/

    html:

    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
    
    
    <input type="text" id="mapaddress" />
    <input type="submit" id="change" onclick="changeMap()">
    <div id="map" style="width: 413px; height: 300px;"></div>
    

    javascript

    <script type="text/javascript">
        function changeMap(){
    
            var mapOptions = {
                zoom: 16,
                center: new google.maps.LatLng(54.00, -3.00),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
    
            var geocoder = new google.maps.Geocoder();
    
            //var address = '3410 Taft Blvd  Wichita Falls, TX 76308';
            var address= document.getElementById("mapaddress").value;
    
            if(!address) {
                address = '3410 Taft Blvd  Wichita Falls, TX 76308';
            }
    
            geocoder.geocode( { 'address': address}, 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);
                }
            });
    
            var map = new google.maps.Map(document.getElementById("map"), mapOptions);
    
            return false;
        }
    </script>
    

    【讨论】:

    • 我想我应该解释一下“帖子标题”;这是一个 Wordpress 帖子标题,而不是在字段中输入地址;)。您的解决方案也对我不起作用。默认情况下没有地图,当我输入详细信息时,地图会变蓝一会儿,然后重新加载页面并且什么都没有显示。至少感谢您尝试解决它!
    • 如果页面重新加载,你在return false之前的js中有错误。默认情况下,它没有定义为运行地图,您可以在准备好的文档上调用 changemap() 来执行此操作。顺便说一句,wp管理页面默认加载jQuery,所以你不妨使用它,你可以用event.preventDefault()等替换return false。标题槽的id是“title”所以你可以使用jQuery (document).on('change','#title' ....etc) 调用上面的函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多