gpt4 book ai didi

c# - map 初始化后将标记添加到谷歌地图

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:48 24 4
gpt4 key购买 nike

我有一个 c# wpf 项目,它使用网络浏览器控件来托管谷歌地图。请注意,这不是一个 asp 项目。

如果我在初始化例程中添加标记,它们就会起作用。如果我稍后使用 InvokeScript 从 C# 调用相同的例程,那么它将不起作用。

我希望有一个页面,我的 C# 代码可以通过调用各种 javascript 函数根据需要添加/删除/更新标记。

我不擅长 javascript,我怀疑有一些我不理解的关键。

相关部分:

<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>

我将 [GLAT]、[GLONG] 和 [ZOOM] 替换为实际坐标和缩放。稍后会详细介绍 [LOCATIONS]。

MarkerWithLabel 是 Gary Little 的代码 http://code.google.com/p/google-maps-utility-library-v3/source/browse/tags/markerwithlabel/1.1.8/src/markerwithlabel.js

如果我用以下内容替换 [MARKERLOCATIONS],我会在页面上得到我想要的标记

AddMarker(51.47,0,"Greenwich")

用不同的坐标重复这个语句可以得到我想要的尽可能多的标记。

如果我删除 [MARKERLOCATIONS] 而不是在 c# 中执行以下操作,则它不起作用(gmap 是 wpf 网络浏览器):

gmap.LoadCompleted += new LoadCompletedEventHandler(gmap_LoadCompleted);

void gmap_LoadCompleted(object sender, NavigationEventArgs e)
{
// 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);
}
}

出于调试目的,我在 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");
}

如果我添加 5 个标记,那么 ReportGPS 会给我 5 个警报。

所以它们就像没有被添加到 map 中一样。同样,如果从初始化函数调用 AddMarker,它会起作用。

我错过了什么?

最佳答案

我认为问题可能是您正在调用 AddMarker initialize 之前的函数函数已经执行。我有一些代码在做一些非常相似的事情,我使用 C# 调用我的 initialize在调用我的 AddMarker 之前的功能功能。

尝试删除 initialze来自 <body onload="initialize()"> 的电话并添加 InvokeScript初始化 map 的函数调用。

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);
}
}

关于c# - map 初始化后将标记添加到谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18637797/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com