使用谷歌地图作为商店定位器

本文关键字:定位器 谷歌地图 | 更新日期: 2023-09-27 18:26:49

所以,我构建了一个简单的商店定位器,花了几个小时试图让它发挥作用,最终意识到问题是谷歌地图不允许你对整个页面进行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>");
    }
}

使用谷歌地图作为商店定位器

在文本搜索中引用GM api,特别是使用"near"时,https://developers.google.com/places/documentation/search#PlaceSearchRequests

如果你想在地图上找到多个搜索结果,这里是你可能需要的东西。谷歌地图多个搜索结果

祝你好运!