生成HTML时,我可以通过简单地调用
轻松地从我需要的JSON对象中获取属性{{cmpyJson[jobCmpyID].company_name}}
然后html将包括"谷歌","亚马逊"或任何其他要求的对象。
然而,当我尝试
时<script>
var company = cmpyJson[jobCmpyID].company_name
</script>
公司被分配了null
。
那么如何将公司价值设置为html可获得的字符串?
这是我想要生成的整个页面:
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Geocoding Simple</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;
var map;
var address = "lamar ";
//address = "austin, tx";
//var address ={{cmpyJson[jobCmpyID].company_name}}
//address +=" Company, ";
address += locJson[jobLocID].location_name;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 11,
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);
}
});
}
}
</script>
</head>
<body style="margin:0px; padding:0px;" onload="initialize()">
<div id="map_canvas" style="width:100%; height:200px;"></div>
</body>
答案 0 :(得分:1)
我认为将公司名称放入HTML然后在您的JS中从HTML解析它是有意义的。例如:
<script>
// …
var address = document.getElementyById('company-name');
// …
</script>
</head>
<body style="margin:0px; padding:0px;" onload="initialize()">
<div id="map_canvas" style="width:100%; height:200px;">
This should be a map showing the location of <span id="company-name">{{cmpyJson[jobCmpyID].company_name}}</span>.
</div>
</body>
这样,当地图无法加载时,你也可以得到一个很好的后备。