我正在JavaScript / ASP.NET中开发一个使用Google地图的应用。我收到以下警告对话框:
"此页面无法显示Google地图元素。提供的 Google API密钥无效或此网站无权使用该密钥。 错误代码:InvalidKeyOrUnauthorizedURLMapError"
此警报是在我从localhost数据库中在地图中创建标记之后发出的。在地图连接到数据库之前,一切正常。
这是javascript代码:
private static string SetSourceScript()
{
StringBuilder sb = new StringBuilder();
string markers = ProductAllLocationCatalog.GetAllLocation();
sb.Append("<script type='text/javascript' src='https://maps.googleapis.com/maps/api/js?v=3&key=MYKEY&sensor=false' runat='server'></script>");
sb.Append("<script type='text/javascript'> ");
sb.Append(" function initialize() { ");
sb.Append(" var mapOptions = { ");
sb.Append(" center : new google.maps.LatLng(-6.9027117, 107.6037877), ");
sb.Append(" zoom : 9, ");
sb.Append(" mapTypeId: google.maps.MapTypeId.ROADMAP ");
sb.Append(" }; ");
sb.Append(" var myMap = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);" + markers + "}");
sb.Append("google.maps.event.addDomListener(window, 'load', initialize); ");
sb.Append("</script> ");
//sb.Append("<script src='../js/MapScript.js' runat='server'></script>");
return sb.ToString();
}
public static string GetSourceScript()
{
return SetSourceScript();
}
<!--Code to call the map :-->
<div id="map-canvas" style="width: 100%; max-width: 950px; height:400px; text-align:center;"></div>
这就是我从数据库中获取纬度和经度的方法:
public static string GetAllLocation()
{
SqlConnection conn = GetConnection();
StringBuilder sqlCmd = new StringBuilder();
string markers = "";
sqlCmd.Append("SELECT Lat, Lng, City FROM csr");
SqlCommand cmd = GetCommand(conn, sqlCmd.ToString());
try
{
conn.Open();
DataTable data = new DataTable();
SqlDataReader reader = GetDataReader(cmd);
int i = 0;
while (reader.Read())
{
i++;
markers +=
@"var marker" + i.ToString() + @" = new google.maps.Marker({" +
@"position : new google.maps.LatLng(" + reader["Lat"].ToString() + ", " + reader["Lng"].ToString() + ")," +
@"map : myMap, title:'" + reader["City"].ToString() + "'});" +
@"google.maps.event.addListener(marker" + i.ToString() + @",'click',function(marker" + i.ToString() + "," + i + ") {" +
@"return function() {" +
@"infowindow.setContent('" + reader["City"].ToString() + "');" +
@"infowindow.open(map, marker" + i.ToString() + ");" +
"}" +
@"})(marker" + i.ToString() + "," + i + "));";
}
return markers;
}
finally
{
conn.Close();
}
}
请帮忙~~谢谢!
顺便说一下,我已经设置了api密钥,所以它允许我的IP地址和引用设置为localhost / *
数据库中的标记工作正常。我可以说,因为地图会在两秒后消失。所以我已经看到标记完美了。
而且,如果我希望标记来自我的数据库,是否必须使用Google Map for Work?如果我想要这张地图可以从许多计算机上访问?