我正在尝试使用我正在构建的webapp来解决范围问题。 名为paths的数组正在填充XML文件中的坐标。我在函数之前创建数组,这样它就是一个全局变量。我能够在整个数组填充坐标后控制整个数组。但我不能通过jQuery运行它每个循环和一个javascript for循环。因为我需要使用google.maps.geometry.spherical.computeDistanceBetween()来计算点之间的距离,所以我之后可以通过循环运行它是非常重要的。
提前致谢! :)
我的代码如下:
$(document).ready(function() {
var newLatLng = new google.maps.LatLng(17.74033553, 83.25067267);
MYMAP.init('#map-img', newLatLng, 3);
MYMAP.placeMarkers('xml/coordinats.xml');
});
var MYMAP = {
map: null,
bounds: null
};
MYMAP.init = function(selector, latLng, zoom) {
var myOptions = {
zoom: zoom,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true, // a way to quickly hide all controls
mapTypeControl: false,
scaleControl: true,
zoomControl: false,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE
},
minZoom: 3
};
this.map = new google.maps.Map($(selector)[0], myOptions);
this.bounds = new google.maps.LatLngBounds();
};
var currentRouteId = <?php echo json_encode($route_id)?>;
var paths = new Array;
var i = 0;
MYMAP.placeMarkers = function(filename) {
$.get(filename, function(xml) {
$(xml).find("koordinator").each(function() {
var routeId = $(this).find('rute_id').text();
var routeIdInt = parseInt(routeId);
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
var point = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
var markerXML = $(this).find('marker').text();
var markerImg = "images/" + markerXML;
if (routeIdInt === currentRouteId) {
paths.push(point);
// extend the bounds to include the new point
MYMAP.bounds.extend(point);
var marker = new google.maps.Marker({
position: point,
map: MYMAP.map,
clickable: false,
options: {icon: new google.maps.MarkerImage(markerImg)}
});
MYMAP.map.fitBounds(MYMAP.bounds);
}
});
});
};
console.log(paths);