所以,我构建了一个简单的商店定位器,花了几个小时试图让它工作,最后意识到问题是谷歌地图不允许你对整个页面进行 iframe。有什么办法可以让它显示吗?或者在没有 iframe 的情况下做我正在做的事情的方法?
理想情况下,如果没有页面的其余部分,只有地图本身会很棒,但我找不到使用不需要数据库或某种数据文件的 api 的方法,而且商店位置太多一直在改变以跟上这一点。
这是我到目前为止所拥有的。
标记:
<%@ Page Title="HelixMix | Where To Buy" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeFile="WhereToBuy.aspx.cs" Inherits="WhereToBuy" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
function reloadFrame(Map) {
document.getElementById(Map).contentDocument.location.reload(true);
}
</script>
<script type="text/javascript">
function selectStore() {
document.write("Please select at least one store."); // Change location on page...
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div id="Locator">
Zip code:
<asp:TextBox ID="TextBox1" placeholder="Zip code" runat="server"></asp:TextBox>
<div class="personalSpace">Which store would you like to search for? Please choose only one.</div>
<ul id="stores">
<li>Store name 1:
<asp:RadioButton ID="RadioButton1" runat="server" />
</li>
<li>Store name 2:
<asp:RadioButton ID="RadioButton2" runat="server" />
</li>
<li>Store name 3:
<asp:RadioButton ID="RadioButton3" runat="server" />
</li>
<li>Store name 4:
<asp:RadioButton ID="RadioButton4" runat="server" />
</li>
<li>Store name 5:
<asp:RadioButton ID="RadioButton5" runat="server" />
</li>
</ul>
<asp:Button ID="Button1" runat="server" Text="Find locations" OnClick="Button1_Click" />
<div>
<iframe id="Map" runat="server"></iframe>
</div>
</div>
</asp:Content>
后面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class WhereToBuy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Map.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
Map.Visible = true;
string zipCode = TextBox1.Text;
string SearchURL = "";
int store = 0;
if (RadioButton1.Checked)
{
store = 1;
}
if (RadioButton2.Checked)
{
store = 2;
}
if (RadioButton3.Checked)
{
store = 3;
}
if (RadioButton4.Checked)
{
store = 4;
}
if (RadioButton5.Checked)
{
store = 5;
}
switch (store)
{
case 1:
SearchURL = "https://www.google.com/maps/preview#!q=gnc+near%3A+";
break;
case 2:
SearchURL = "https://www.google.com/maps/preview#!q=gnc+near%3A+"; //Change store name
break;
case 3:
SearchURL = "https://www.google.com/maps/preview#!q=gnc+near%3A+"; //Change store name
break;
case 4:
SearchURL = "https://www.google.com/maps/preview#!q=gnc+near%3A+"; //Change store name
break;
case 5:
SearchURL = "https://www.google.com/maps/preview#!q=gnc+near%3A+"; //Change store name
break;
default:
Page.RegisterClientScriptBlock("Select store", "<script>\n selectStore(); \n </script>"); //Rewrite flow to not load iframe if this executes... Back to new search...
break;
}
Map.Attributes.Add("src", SearchURL + zipCode);
Page.RegisterClientScriptBlock("Reload", "<script>\n reloadFrame('Map'); \n </script>");
}
}