C# 在地图初始化后将标记添加到google地图
我有一个c#wpf项目,使用webbrowser控件托管一个google地图。注意,这不是一个asp项目 如果我在初始化例程中添加标记,它们就会工作。如果我稍后使用InvokeScript从c#调用相同的例程,那么它就不起作用 我希望有一个页面,我的c#代码可以根据需要通过调用各种javascript函数来添加/删除/更新标记 我不擅长javascript,我怀疑有一些关键的东西我不理解 相关部分:C# 在地图初始化后将标记添加到google地图,c#,javascript,wpf,google-maps-api-3,C#,Javascript,Wpf,Google Maps Api 3,我有一个c#wpf项目,使用webbrowser控件托管一个google地图。注意,这不是一个asp项目 如果我在初始化例程中添加标记,它们就会工作。如果我稍后使用InvokeScript从c#调用相同的例程,那么它就不起作用 我希望有一个页面,我的c#代码可以根据需要通过调用各种javascript函数来添加/删除/更新标记 我不擅长javascript,我怀疑有一些关键的东西我不理解 相关部分: <script type="text/javascript" src="http://ma
<script type="text/javascript" src="http://maps.google.com.mx/maps/api/js?sensor=false&language="es""></script>
<script type="text/javascript">
var markersArray = [];
var gmap;
function initialize() {
var latlng = new google.maps.LatLng([GLAT], [GLONG]);
var myOptions = {
zoom: [ZOOM],
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
gmap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
[MARKERLOCATIONS]
}
function AddMarker(Latitude, Longitude, Description) {
var latlng = new google.maps.LatLng(Latitude, Longitude);
var marker = new MarkerWithLabel({
position: latlng,
map: gmap,
title: Description,
labelContent: "1",
labelAnchor: new google.maps.Point(7, 30),
labelClass: "labels",
labelInBackground: false
});
</script>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</script>
用不同的坐标重复这句话会给我想要的标记
如果我删除[MARKERLOCATIONS]并在c#中执行以下操作,它将不起作用(gmap是wpf webbrowser):
出于调试目的,我在AddMarker中放置了一个警报,然后依次传递值;我还创建了一个函数来循环markersArray的内容:
function ReportGPS() {
if (markersArray) {
for (var i = 0; i < markersArray.length; i++) {
//remove the marker from the map
alert(markersArray[i].toString());
}
}
}
public void Test()
{
object dummy = gmap.InvokeScript("ReportGPS");
}
函数报告GPS(){
if(markersArray){
对于(var i=0;i
如果我加上5个标记,那么ReportGPS会给我5个警报
这就好像他们根本没有被添加到地图上。同样,如果从initialise函数调用AddMarker,它也会工作
我遗漏了什么?我认为问题可能是您在执行
初始化
函数之前调用了AddMarker
函数。我有一些代码正在做一些非常类似的事情,在调用我的AddMarker
函数之前,我使用C#调用我的initialize
函数
尝试从
中删除initialize
调用,并添加InvokeScript
函数调用以初始化映射
void gmap_LoadCompleted(object sender, NavigationEventArgs e)
{
gmap.InvokeScript("initialize");
// Add markers
foreach (Marker myM in ListMarker)
{
Object[] CallArgs = new Object[3];
CallArgs[0] = myM.Latitude.ToString();
CallArgs[1] = myM.Longitude.ToString();
CallArgs[2] = myM.ShortDescription;
object dummy = gmap.InvokeScript("AddMarker", CallArgs);
}
}
function ReportGPS() {
if (markersArray) {
for (var i = 0; i < markersArray.length; i++) {
//remove the marker from the map
alert(markersArray[i].toString());
}
}
}
public void Test()
{
object dummy = gmap.InvokeScript("ReportGPS");
}
void gmap_LoadCompleted(object sender, NavigationEventArgs e)
{
gmap.InvokeScript("initialize");
// Add markers
foreach (Marker myM in ListMarker)
{
Object[] CallArgs = new Object[3];
CallArgs[0] = myM.Latitude.ToString();
CallArgs[1] = myM.Longitude.ToString();
CallArgs[2] = myM.ShortDescription;
object dummy = gmap.InvokeScript("AddMarker", CallArgs);
}
}