gpt4 book ai didi

javascript - 使用 Google Maps、C#、ASP.net、SQL Server 显示带标记的 map 。 map 未显示

转载 作者:行者123 更新时间:2023-12-02 14:40:50 25 4
gpt4 key购买 nike

我知道有很多与此相关的问题,但我的问题非常具体。我正在使用在线文章中的示例。这是我的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GoogleMapsAPItesting.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Google Maps Markers</title>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=InsertAPIKeyhere&sensor=false">
</script>

<script type="text/javascript">
function initialize() {



}
</script>
</head>
<body onload="initialize()">
<form id="WebForm1" runat="server">
<div id="mapArea" style="width: 500px; height: 500px;">
</div>


<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</form>
</body>
</html>

以及 C# 背后的代码

   using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace GoogleMapsAPItesting
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string markers = GetMarkers();
Literal1.Text = @"
<script type=’text/javascript’>
function initialize() {

var mapOptions = {
center: new google.maps.LatLng(28.3213, 77.5435),
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var myMap = new google.maps.Map(document.getElementById(‘mapArea’),
mapOptions);" + markers + @"}
</script>";
}



protected string GetMarkers()
{
string markers = "";
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source=FakeServer;" +
"Initial Catalog=FakeInitCatalog;" +
"User id=FakeUID;" +
"Password=FakePass;";
{
SqlCommand cmd = new SqlCommand("SELECT Latitude, Longitude, City FROM Locations", conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
int i = 0;

while (reader.Read())
{
i++;
markers +=
@"var marker" + i.ToString() + @" = new google.maps.Marker({
position: new google.maps.LatLng(" + reader["Latitude"].ToString() + ", " +
reader["Longitude"].ToString() + ")," +
@"map: myMap,
title:’" + reader["City"].ToString() + "’});";
}
}
return markers;
}
}
}

当我运行它时, map 没有显示,只是一个空白页。如果我采用 Literal1 的 C# 代码,并将其放置在 HTML/Asp 页面中,则会加载 map (但不会放置标记)。我感觉我只缺了一 block 。我非常感谢任何建议。

谢谢。

最佳答案

看来您使用了打印单引号“’”而不是编程单引号“’”。

参见

...
script type=’text/javascript’
...

document.getElementById(‘mapArea’)
...

title:’" + reader["City"].ToString() + "’

以下修改后的代码可以工作

protected void Page_Load(object sender, EventArgs e)
{
string markers = GetMarkers();
Literal1.Text = @"
<script type='text/javascript'>
function initialize() {

var mapOptions = {
center: new google.maps.LatLng(28.3213, 77.5435),
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var myMap = new google.maps.Map(document.getElementById('mapArea'),
mapOptions);" + markers +
@"
}
</script>";

}


protected string GetMarkers()
{
string markers = "";

float latitude = 28.3213f;
float longitude = 77.5435f;

for (var i = 0; i < 10; i++)
{
i++;
markers +=
@"var marker" + i.ToString() + @" = new google.maps.Marker({
position: new google.maps.LatLng(" + (latitude + i).ToString() + ", " +
(longitude + i).ToString() + ")," +
@"map: myMap,
title:'" + "City" + i.ToString() + "'});";
}

return markers;
}

关于javascript - 使用 Google Maps、C#、ASP.net、SQL Server 显示带标记的 map 。 map 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37032074/

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