使用谷歌地图API发布位置标记
本文关键字:位置 布位置 谷歌地图 API | 更新日期: 2023-09-27 18:20:06
我正在使用Google Maps API来显示.Net平台上的位置标记。我已经将纬度、经度、城市和州的信息存储在数据库中,如果我添加纬度和经度的第7个数据库条目,地图和标记将无法显示。
下面是.aspx的代码和背后的代码
.aspx:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
Google Map Locations
</title>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=My_Key&sensor=false">
</script>
</head>
<body onload="initialize()">
<form id="form1" runat="server">
<div id="mapArea" style="width: 700px; height: 700px;">
</div>
<asp:Label ID="Label11" runat="server"></asp:Label>
</form>
</body>
</html>
代码隐藏:
namespace GoogleMaps
{
public partial class GoogleMaps : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string markers = GetLocationMarkers();
Label1.Text = @"
<script type='text/javascript'>
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(39.948427, -101.836428),
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var myMap = new google.maps.Map(document.getElementById('mapArea'),
mapOptions);"
+ markers +
@"}
</script>";
}
protected string GetLocationMarkers()
{
string markers = "";
using (SqlConnection con = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
{
SqlCommand cmd = new SqlCommand(
"SELECT Latitude, Longitude, City FROM Map", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
int i = 0;
while (reader.Read())
{
i++;
markers += @"var marker"+i.ToString()+ @" =
new google.maps.Marker({
position:google.maps.LatLng("+reader["Latitude"].ToString() +"
, "+
reader["Longitude"].ToString() +")," +
@"map: myMap,title:'"+reader["City"].ToString()+
"'});";
}
}
return markers;
}
}
}
在调试时,我发现,在Marker 7上,我看到''r''n在城市名称之后,在下面的调试输出中,示例:在Pipestone 之后
markers "var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(32.692383, -114.626648),
map: myMap, title:'Yuma'});
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(38.250095, -122.040001),
map: myMap, title:'Fairfield'});
var marker3 = new google.maps.Marker({
position: new google.maps.LatLng(26.56225, -81.948121),
map: myMap, title:'Cape Coral'});
var marker4 = new google.maps.Marker({
position: new google.maps.LatLng(45.52263, -122.681208),
map: myMap, title:'Portland'});
var marker5 = new google.maps.Marker({
position: new google.maps.LatLng(37.810308, -85.981012),
map: myMap, title:'Vine Grove'});
var marker6 = new google.maps.Marker({
position: new google.maps.LatLng(28.540228, -81.385238),
map: myMap, title:'Orlando'});
var marker7 = new google.maps.Marker({
position: new google.maps.LatLng(43.99878, -96.313606),
map: myMap, title:'Pipestone'r'n'});" string
但是当数据库中只有6个条目在城市名称后我看不到时,例如:在下面的调试输出中:在Orlando 之后
markers "var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(32.692383, -114.626648),
map: myMap, title:'Yuma'});
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(38.250095, -122.040001),
map: myMap, title:'Fairfield'});
var marker3 = new google.maps.Marker({
position: new google.maps.LatLng(26.56225, -81.948121),
map: myMap, title:'Cape Coral'});
var marker4 = new google.maps.Marker({
position: new google.maps.LatLng(45.52263, -122.681208),
map: myMap, title:'Portland'});
var marker5 = new google.maps.Marker({
position: new google.maps.LatLng(37.810308, -85.981012),
map: myMap, title:'Vine Grove'});
var marker6 = new google.maps.Marker({
position: new google.maps.LatLng(28.540228, -81.385238),
map: myMap, title:'Orlando'});
是什么原因导致谷歌地图没有出现?数据库中只有6个条目,显示带有标记的地图并没有问题。任何答复都将不胜感激。
可能是转义序列问题尝试仅选择第7个城市Piperstone
以检查问题是否与字符串内容有关。在此标记中,si不显示,尝试更新此条目,删除"''r''n"字符