我目前正在使用谷歌地图,并且我有一种在谷歌地图上创建标记的方法。该方法给出了标记的信息。此信息的一部分是标记的经度和纬度。经度和纬度是通过 javascript 中的函数从邮政编码中检索的。但是,当我返回经度和纬度时,它说它不包含任何内容。
它获取多个邮政编码,并应返回多个经度和纬度
我的代码:
这会添加标记。
void AddMarkerTimer(object sender, EventArgs e)
{
try
{
this.Document.InvokeScript("addMarker", new string[] { this.Document.InvokeScript("getLatFromPost", new string[] { delictMarkers[MarkerIndex].PostalCode }) + "", this.Document.InvokeScript("getLonFromPost", new string[] { delictMarkers[MarkerIndex].PostalCode }) + "", "marker" + MarkerIndex, MarkerIndex + "" });
MarkerIndex++;
}
catch(ArgumentOutOfRangeException ax){
MessageBox.Show(ax.ToString());
}
if (MarkerIndex == delictMarkers.Count - 1)
{
MarkerTimer.Stop();
}
}
这应该返回经度和纬度 (javascript)。
function getLatFromPost(post) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: post },
function (results_array, status) {
var s;
s = results_array[0].geometry.location.lat();
//alert(s);
// Check status and do whatever you want with what you get back
// in the results_array variable if it is OK.
return s;
});
}
function getLonFromPost(post) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: post },
function (results_array, status) {
var s;
s = results_array[0].geometry.location.lng();
alert(s);
// Check status and do whatever you want with what you get back
// in the results_array variable if it is OK.
return s;
});
}
这部分将其添加到 map 上(javascript)。
function addMarker(x, y, titleMarker, index) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(x, y),
map: map,
title: titleMarker,
icon: delict,
index: index
});
google.maps.event.addListener(marker, "click", function (event) {
showMarkerContextMenu(event.latLng, index, titleMarker);
});
marker.set("index", index);
markerList[index] = marker;
//marker.setMap(map);
}
我希望大家清楚我的问题是什么。 预先感谢您的关注。
最佳答案
geocoder.geocode
异步工作。这意味着您传递参数和回调函数。当答案准备好时,回调函数将被调用。所以基本上你的回调函数返回一个值是没有意义的。因为即使要返回该值,它也会返回给调用者,在本例中是 google。
关于c# - 将字符串从 javascript 返回到 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20204353/