我在谷歌地图上创建了一些标记。我从c#
调用了创建标记功能 for(int a = 0; a<cpt;a++)
{
jsFunc = "myglobalObject.createMarker(" + nearestStations[a][0] + "," + HttpUtility.JavaScriptStringEncode("test", true) + ")";
ScriptManager.RegisterClientScriptBlock(this, GetType(), "script" + a, "$(function () {" + jsFunc + "; });", true);
}
我的js看起来像这样:
var markers = [];
var infoWindowIsSet = false;
myglobalObject = {
map: false,
prev_infowindow: false,
initializeMap: function () {
$(document).ready(function () {
var adresseConnexion = document.getElementById("tbAdresseDefault").value;
var geocoder = new google.maps.Geocoder();
mapOptions = {
zoom: 6,
center: { lat: 46, lng: 1 },
language: "en-US",
mapTypeId: google.maps.MapTypeId.ROADMAP
}
myglobalObject.map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
geocoder.geocode({ 'address': adresseConnexion }, function (results, status) {
if (status == 'OK') {
myglobalObject.map.setCenter(results[0].geometry.location);
myglobalObject.createMarker(results[0].geometry.location, "test");
console.log(results[0].geometry.location);
} else {
alert('Adresse incorrecte ');
}
});
});
},
createMarker: function (position, information) {
marker = new google.maps.Marker({
position: position,
map: myglobalObject.map,
});
markers.push(marker);
google.maps.event.addListener(marker, "click", function () {
infowindow = new google.maps.InfoWindow({ content: information });
if (myglobalObject.prev_infowindow) {
myglobalObject.prev_infowindow.close();
}
myglobalObject.prev_infowindow = infowindow;
infowindow.open(myglobalObject.map, this);
infoWindowIsSet = true;
});
google.maps.event.addListener(myglobalObject.map, "click", function () {
if (infoWindowIsSet)
infowindow.close();
});
return marker;
}
}
正如你所看到的,我在&#34; myglobalObject&#34;中有一个map变量。我在这里设置:
myglobalObject.map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
然而,在从C#调用createrMarker函数后,变量映射似乎被重置为&#34; false&#34;所以我在createMarker中需要它的部分都失败了。我试图将地图作为全局变量,但仍然失败了。我该怎么办?
.cs代码:
public string[][] nearestStations = new String[100][];
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
parsingXMLDistance();
}
public void parsingXMLDistance()
{
double latOfYourStation = double.Parse(GetLatLng(tbAdresseDefault.Text)[0], CultureInfo.InvariantCulture);
double lngOfYourStation = double.Parse(GetLatLng(tbAdresseDefault.Text)[1], CultureInfo.InvariantCulture);
string latStr, lngStr;
int cpt = 0;
for (int j = 0; j < 100; j++)
nearestStations[j] = new String[1];
string jsFunc;
PDV.pdv_liste l = new PDV.pdv_liste();
l = (litXMLListe("myXMLFile"));
for (int i = 0; i <l.pdv.Length; i++)
{
System.Diagnostics.Debug.Write(i + "\r\n");
if (l.pdv[i].latitude != "" && l.pdv[i].longitude != "") {
if (DistanceTo(latOfYourStation, lngOfYourStation, double.Parse(l.pdv[i].latitude, CultureInfo.InvariantCulture) / 100000, double.Parse(l.pdv[i].longitude, CultureInfo.InvariantCulture) / 100000) < double.Parse(tbDistDefault.Text, CultureInfo.InvariantCulture) )
{
latStr = "" + double.Parse(l.pdv[i].latitude, CultureInfo.InvariantCulture) / 100000;
lngStr = "" + double.Parse(l.pdv[i].longitude, CultureInfo.InvariantCulture) / 100000;
latStr = latStr.Replace(',', '.');
lngStr = lngStr.Replace(',', '.');
nearestStations[cpt][0] = "{lat:" + latStr + ",lng:" + lngStr + "}";
cpt++;
}
}
}
for(int a = 0; a<cpt;a++)
{
jsFunc = "myglobalObject.createMarker(" + nearestStations[a][0] + "," + HttpUtility.JavaScriptStringEncode("test", true) + ")";
ScriptManager.RegisterClientScriptBlock(this, GetType(), "script" + a, "$(function () {" + jsFunc + "; });", true);
}
}