マップのロードマーカーを作成しようとしています。これはXMLファイルから取り込まれました。現在、これは完全にこれを行います。しかし、ハイパーリンクから渡された月に基づいて新しいマーカをリロードすることも欲しい。Google Maps API v3 - Ajax(Javascript + Jquery)GET issue
ハイパーリンクを介して変数 'global_month'を変更すると、後で 'if'ステートメントに渡された日付に基づいてajax要求が発生し、 'data_array'に新しいデータが再投入されます。ただし、 'data_array'のコンテンツは再利用され、新しいデータで埋められません。
私は 'change_date_range'関数で 'global_month = []'を介して 'global_month'をリセットしようとしましたが、配列は再充填されていないようです。私は3時間過ごして、私が間違っている場所を見つけようとしましたが、できません。誰も助けることができますか?これを行うより効率的な方法もありますか?
ありがとうございました!
///////////////////////////////////////////////////
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "driving_test_centers.xml",
dataType: "xml",
success: parseXml
});
});
var global_month = 6;
function refetch()
{
$.ajax({
type: "GET",
url: "driving_test_centers.xml",
dataType: "xml",
success: parseXml
});
};
var data_array = [];
var empty_holder;
var empty_month;
var info_date;
var address1;
var address2;
var town;
var county;
var postcode;
var lat;
var lon;
var male_pass_percentage1;
var male_pass_percentage;
var female_pass_percentage1;
var female_pass_percentage;
var the_new_date;
var infowindow;
var xml_contents;
function parseXml(xml)
{
$(xml).find("test_center").each(function() // look for all of the test centers
{
center_name = $(this).attr('name');
address1 = $(this).find('address1').text();
address2 = $(this).find('address2').text();
town = $(this).find('town').text();
county = $(this).find('county').text();
postcode = $(this).find('postcode').text();
lat = $(this).find('lat').text();
lon = $(this).find('lon').text();
$(this).find("date_info").each(function()
{
info_date = $(this).attr('date');
empty_month = $(this).find('month').text();
male_pass_percentage = $(this).find('male');
male_pass_percentage = male_pass_percentage.find('pass_percentage').text();
female_pass_percentage = $(this).find('female');
female_pass_percentage = female_pass_percentage.find('pass_percentage').text();
if(empty_month == global_month){
data_array.push([lat,lon,center_name,info_date,male_pass_percentage,female_pass_percentage]);
}
});
});
}
function change_date_range(the_new_date){
global_month = the_new_date;
refetch();
init();
};
</script>
<script>
function init() {
var options = {
zoom: 5,
center: new google.maps.LatLng(55.3781, -3.4360),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var places;
places = null;
var map = new google.maps.Map(document.getElementById('map'), options);
var bounds = new google.maps.LatLngBounds();
var places = data_array;
var temp_location;
for (var i = 0; i < places.length; i++) {
temp_location = new google.maps.LatLng(places[i][0], places[i][1])
// Adding the markers
var marker = new google.maps.Marker({
position: temp_location,
map: map,
title: places[i][2]
});
// Wrapping the event listener inside an anonymous function
// that we immediately invoke and passes the variable i to.
(function(i, marker) {
// Creating the event listener. It now has access to the values of
// i and marker as they were during its creation
google.maps.event.addListener(marker, 'click', function() {
// Check to see if we already have an InfoWindow
if (!infowindow) {
infowindow = new google.maps.InfoWindow();
}
// Setting the content of the InfoWindow
infowindow.setContent(places[i][2] + "<br />" + global_month + "<br />" + places[i][3] + "<br /><br />Male: " + places[i][4] + "%<br />Female: " + places[i][5] + "%");
// Tying the InfoWindow to the marker
infowindow.open(map, marker);
});
})(i, marker);
// Extending the bounds object with each LatLng
bounds.extend(temp_location);
}
// Adjusting the map to new bounding box
map.fitBounds(bounds)
};
</script>
</head>
<body onload="init()">
<div id="map" style="width:800px;height:600px"></div>
<a href="javascript:change_date_range(7)">click</a>
</body>
</html>
///////////////////////////////////////////////////////////////////////////////// /////////////////
XMLファイル(のごく一部):
http://tinypaste.com/198889bb
data_array = []を追加しようとすると、リンクがクリックされたときに現在の月が読み込まれます月が過ぎたそれは二度目には機能しますが、毎回地図をリロードしているので問題が発生していると思います。 XMLコンテンツを配列に格納することは可能ですか?それが問題の原因になりますか? – Chris
おかげさまで、あなたは私に出発点を与えました。私は全体を書き直し、もっとうまく働きました。乾杯! – Chris
偉大な、私は同じことをやっていた。私は理想的にはXMLを一度解析し、それを一意の配列に分割し、目標月を変更してマップをクリアし、既存の配列の1つをループするといいでしょう。同じデータを1回以上ajaxする必要はありません(ただし、月のパラメータを受け取り、全体のデータセットの一部のみを与えるスクリプトになるようにそのターゲットを変更することもできます)。それを複数回呼び出す)。あなたはそれを軌道に乗せてうれしいです。 –