[英]Parsing Geocode from XML into JavaScript
Currently I have a page displaying 3 Google Maps that are generated by JavaScript, these are based on a geocode that is hard coded into the JavaScript. 目前,我有一个页面,显示3个由JavaScript生成的Google地图,这些地图基于硬编码到JavaScript中的地理编码。
I now have set the geocodes within an XML file and using a PHP function to call the geocode from the XML into a array. 现在,我已经在XML文件中设置了地理编码,并使用PHP函数将地理编码从XML调用到数组中。 But I'm unsure on how to go about parsing this from the array into the JavaScript.
但是我不确定如何将其从数组解析为JavaScript。
Here is my JavaScript. 这是我的JavaScript。
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js? sensor=true&libraries=places"></script>
<script type="text/javascript">
function initMap(mapObj, lat, lng) {
var latLng = new google.maps.LatLng(lat, lng);
var mapInstance = new google.maps.Map(mapObj, {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: latLng,
zoom: 11
});
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(mapInstance);
function createMarker (place) {
var marker = new google.maps.Marker({
animation: google.maps.Animation.DROP,
map: mapInstance,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(mapInstance, this);
});
}
function searchCallback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
service.search({ location: latLng, radius: 5000 }, searchCallback);
}
function initialize() {
var ndx = 0;
function calInitMap(lat, lng) {
initMap(document.getElementById('map'+ndx), lat, lng);
ndx++;
}
calInitMap(51.5069999695, -0.142489999533);
calInitMap(40.79445,-74.01558);
calInitMap(48.858001709, 2.29460000992);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<noscript>JavaScript not detected! Verify that JavaScript support is enabled in your browser, or upgrade to a newer JavaScript-capable web browser and try again.</noscript>
<div id="map0" style="position:absolute; width: 290px; height: 300px;"></div>
<div id="map1" style="position:absolute; left:492px; width: 288px; height: 300px;"></div>
<div id="map2" style="position:absolute; left:788px; width: 290px; height: 300px;"></div>
Here is the PHP function I'm using to call in the geocode from the XML file. 这是我用来从XML文件中调用地理编码的PHP函数。
function getGeocode() {
try {
$xml = new SimpleXmlElement(file_get_contents('config/cityConfig.xml'));
} catch (Exception $e) {
echo 'Caught exception: An error has occured. Unable to get cityConfig.xml';
}
$geoSource = array();
foreach ($xml->city as $city) {
$geo = (string) $city->geocode;
array_push($geoSource, $geo);
}
return $geoSource;
}
?>
I have thought about using calInitMap($geocodeSource[0]);
我已经考虑过使用
calInitMap($geocodeSource[0]);
, or would it be better to use a JavaScript function to access the XML file? ,还是使用JavaScript函数访问XML文件会更好? Any suggestions?
有什么建议么?
Instead of using php to parse the XML, why not just keep it all in JavaScript using something like $.xmlToJSON: http://www.terracoder.com/index.php/xml-objectifier/xml-objectifier-documentation 除了使用php解析XML之外,为什么不使用$ .xmlToJSON之类的东西将其全部保留在JavaScript中: http : //www.terracoder.com/index.php/xml-objectifier/xml-objectifier-documentation
$.get('/config/cityConfig.xml', function( data ){
var json = $.xmlToJSON( data );
// Do things with JSON here...
});
If jQuery's not your game, you might want to try out this script: http://www.thomasfrank.se/xml_to_json.html 如果jQuery不是您的游戏,则可能需要尝试以下脚本: http : //www.thomasfrank.se/xml_to_json.html
You may store the array as a variable in JS: 您可以将数组存储为JS中的变量:
var geocodes=<?php echo json_encode(getGeocode());?>
...no need for parsing on JS-side. ...无需在JS端进行解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.