动态标记无法使用新变量

时间:2015-01-13 06:54:42

标签: javascript php google-maps dynamic google-maps-markers

var latitude = document.getElementById("latitude");
var longitude = document.getElementById("longitude");

var myCenter = new google.maps.LatLng(latitude, longitude);
var marker;

function initialize()
{
    var mapProp = {
        center: myCenter,
        zoom: 5,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

    marker = new google.maps.Marker({
        position: myCenter,
        animation: google.maps.Animation.BOUNCE
    });

    marker.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);

var latitude没有获得值

这里我从下面的ID中获取值,

<div class="map" id="googleMap" style="width:100%;height:260px;overflow:hidden;"></div>
<div id="latitude"><?php echo $itemData['130']; ?></div>
<div id="longitude"><?php echo $itemData['131']; ?></div>

$itemData['130']$itemData['130']中,价值正常,

我不知道我做错了什么?

4 个答案:

答案 0 :(得分:3)

你得到的对象。如果您想要PHP正在回显的值,请使用.innerHTML

var latitude = document.getElementById("latitude").innerHTML;

答案 1 :(得分:3)

This code will work try this

function initialize() {
var latitude = document.getElementById("latitude").innerHTML;
var longitude = document.getElementById("longitude").innerHTML;
var myCenter = new google.maps.LatLng(latitude, longitude);
var marker;
var mapProp = {
center: myCenter,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
marker = new google.maps.Marker({
position: myCenter,
animation: google.maps.Animation.BOUNCE
});
marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);

答案 2 :(得分:2)

  1. 你需要在渲染DOM之后获取HTML div元素的内容(在初始化函数中)
  2. document.getElementById返回一个HTML元素,你需要文本内容(我使用geoxml3中的nodeValue函数来获取它,但JQuery和其他库中提供了类似的功能。)
  3. working fiddle

    代码段:

    &#13;
    &#13;
    var marker;
    
    function initialize()
    {
    var latitude = nodeValue(document.getElementById("latitude"));
    var longitude = nodeValue(document.getElementById("longitude"));
    
    var myCenter = new google.maps.LatLng(latitude, longitude);
        var mapProp = {
            center: myCenter,
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
    
        var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
    
        marker = new google.maps.Marker({
            position: myCenter,
            animation: google.maps.Animation.BOUNCE
        });
    
        marker.setMap(map);
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    //nodeValue: Extract the text value of a DOM node, with leading and trailing whitespace trimmed
    var nodeValue = function(node, defVal) {
      var retStr="";
      if (!node) {
        return (typeof defVal === 'undefined' || defVal === null) ? '' : defVal;
      }
       if(node.nodeType==3||node.nodeType==4||node.nodeType==2){
          retStr+=node.nodeValue;
       }else if(node.nodeType==1||node.nodeType==9||node.nodeType==11){
          for(var i=0;i<node.childNodes.length;++i){
             retStr+=arguments.callee(node.childNodes[i]);
          }
       }
       return retStr;
    };
    &#13;
    html, body, #googleMap {
        height: 100%;
        width: 100%;
        margin: 0px;
        padding: 0px
    }
    &#13;
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="googleMap" style="border: 2px solid #3872ac;"></div>
    <div id="latitude">45</div>
    <div id="longitude">-85</div>
    &#13;
    &#13;
    &#13;

答案 3 :(得分:0)

检查一下:

var latitude = document.getElementById("latitude").innerHTML;
var longitude = document.getElementById("longitude").innerHTML;

注意:这个将产生纬度和经度。您使用的那个将产生对象而不是值。