使用谷歌地图API获取商家地址

16

我有一个要求,希望Google地图API能够提供解决方案。我从未使用过Google Maps API - 所以我非常陌生。

在网站首页有一个表单,当用户访问时,我希望发生以下情况:

1)根据IP填充城市字段中的用户所在城市

2)有第二个名为“商店名称”的字段 - 当用户开始输入商店名称时,我希望获取该城市中所有具有该名称的商家列表,并将其显示为下拉菜单,用户可以从中选择合适的分支机构。不需要在地图上显示任何内容。 例如 - 如果休斯顿的用户开始输入麦当劳,则应开始显示以下商家列表:

  • 麦当劳, 12 Pearland Ave, Houston TX
  • 麦当劳, 2600 Bary Area Blvd, Houston TX
  • 麦当劳, 262 Clearlake Blvd, Houston TX

此外,当我们从Google API获得商家地址时 - 我们会得到一个字符串还是不同的字段,如街道名称、城市名称、州/省份、邮政编码等?

任何信息或示例将非常感激 谢谢


我添加了一个示例,使用客户端位置和本地搜索来获取靠近客户端的企业,供您和其他寻找此类信息的人参考。 - Sky Sanders
3个回答

15

我认为您可能不需要使用谷歌地图。首先,使用条款不允许除在公共网页上显示谷歌地图之外的其他用途;其次,还有另一个完全符合您需求的谷歌API:客户端位置API:http://code.google.com/apis/ajax/documentation/#ClientLocation

关于“企业”方面的问题:您需要从某个地方获取这些数据 - 我不认为谷歌提供该服务。也许您可以简单地使用谷歌搜索API和一些逻辑来查找只有企业信息的结果(http://code.google.com/apis/ajaxsearch/

编辑:针对企业信息,也许您可以参考以下示例:http://code.google.com/apis/ajaxsearch/samples.html#local-search


1
你好,Roland 感谢您的回复。当我说“商业”时,我指的是已经在Google本地搜索中显示的企业。例如,如果您在http://maps.google.co.in/上搜索McDonalds Houston,它将向您显示以下结果: 2017 Main Street, Houston, TX, United States‎ 808 Dallas Street, Houston, TX, United States 4005 Elgin Street, Houston, TX, United States因此,如果我传递“MCdonalds”和“Houston”,我们无法从Google获取此信息。 - Gublooo
抱歉,我没有看到你的编辑注释。现在我正在查看它。 - Gublooo
1
我可以使用http://code.google.com/apis/ajax/playground/#center_localsearch,但它不会在地图上显示,并且会给我商业地址。是否有任何条款规定我不能将其用于除在地图上显示之外的其他目的?谢谢。 - Gublooo
只要你不使用谷歌地图API,那么你就是可以的。我提到的API不是谷歌地图API。当然,你应该跟随我提供的链接并阅读任何使用条款/服务,并查看它们是否与你的应用程序兼容。 - Roland Bouman
3
看起来这些API和/或文档已经发生了变化,因此这些链接不再起作用。你知道最佳的当前解决方案是什么吗? - Max Ghenis
使用Places API时是否遇到任何限制问题?看起来很简单,但不想在有硬性限制的情况下构建应用。 - Rarw

3

更新:以下是使用Google ClientLocation API和jsonp进行本地搜索的示例。


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAALDWeTDQHOJCbCf0JnUqL8BT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQA7AE8xB9MyWgHECPY2qimOp7BUQ"></script>

    <script src="scripts/clientLocation.js" type="text/javascript"></script>

    <script src="scripts/localSearch.js" type="text/javascript"></script>

    <script type="text/javascript">



        function $g(id) {
            return document.getElementById(id);
        }

        function displayLocation(latitudeEl, longitudeEl, cityEl, regionEl, countryEl, country_codeEl) {
            var cloc = new ClientLocation.Location(google.loader.ClientLocation);
            if (latitudeEl) latitudeEl.innerHTML = cloc.latitude;
            if (longitudeEl) longitudeEl.innerHTML = cloc.longitude;
            if (cityEl) cityEl.innerHTML = cloc.address.city;
            if (regionEl) regionEl.innerHTML = cloc.address.region;
            if (country) country.innerHTML = cloc.address.country;
            if (country_codeEl) country_codeEl.innerHTML = cloc.address.country_code;
        }
        function localSearch(term, callback, context) {
            var cloc = new ClientLocation.Location(google.loader.ClientLocation);
            var searchUrl = 'http://www.google.com/uds/GlocalSearch?callback=' + callback + '&context=' + context + '&hl=en&q=' + encodeURIComponent(term) + '&sll=' + cloc.latitude + ',' + cloc.longitude + '&key=ABQIAAAALDWeTDQHOJCbCf0JnUqL8BT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQA7AE8xB9MyWgHECPY2qimOp7BUQ&v=1.0';

            // http://jaybyjayfresh.com/2007/09/17/using-script-tags-to-do-remote-http-calls-in-javascript/
            scriptLoaded = function() {
                removeNode(newScript);
            };

            var headID = document.getElementsByTagName("head")[0];
            var newScript = document.createElement('script');
            newScript.type = 'text/javascript';
            newScript.onload = scriptLoaded;
            newScript.src = searchUrl;
            headID.appendChild(newScript);
        }
        function search() {
            var term = $g("txtSearch").value;
            localSearch(term, "displayResults", "0");

        }
        function displayResults(context, results, status, details, unused) {
            var titles = [];
            for (var i = 0; i < results.results.length; i++) {
                // this cast is not necessary, just here to illustrate
                // vs intellisense and reduce coding errors.
                var result = new LocalSearch.Result(results.results[i]);
                titles.push(result.title);
            }
            $g("searchResults").innerHTML = titles.join("</br>");
        }
        function init() {

            displayLocation($g("latitude"), $g("longitude"), $g("city"), $g("region"), $g("country"), $g("country_code"));
        }
    </script>

</head>
<body onload="init()">
    <form id="form1" runat="server">
    <div>
        latitude : <span id="latitude"></span>
        <br />
        longitude : <span id="longitude"></span>
        <br />
        city : <span id="city"></span>
        <br />
        region : <span id="region"></span>
        <br />
        country : <span id="country"></span>
        <br />
        country_code : <span id="country_code"></span>
        <br />
    </div>
    <input type="text" id="txtSearch" /><input type="button" id="btnSearch" value="get results"
        onclick="search();" /><br />
    &nbsp;<div id="searchResults">
    </div>
    </form>
</body>
</html>

// <copyright file="clientLocation.js" company="Sky Sanders">
// This source is placed in the Public Domain.
// http://skysanders.net/subtext
// Attribution is appreciated.
// </copyright>


/*
object literal format for google.loader.clientlocation  
{
"latitude": 33.324,
"longitude": -111.867,
"address": {
"city": "Chandler",
"region": "AZ",
"country": "USA",
"country_code": "US"
}
}
*/

var ClientLocation = {};

ClientLocation.Address = function() {
    /// <field name="city" type="String" />
    /// <field name="region" type="String" />
    /// <field name="country" type="String" />
    /// <field name="country_code" type="String" />
    /// <returns type="ClientLocation.Address"/>
    if (arguments.length > 0) {
        this.city = arguments[0].city;
        this.region = arguments[0].region;
        this.country = arguments[0].country;
        this.country_code = arguments[0].country_code;
        return;
    }
    else {
        this.city = "";
        this.region = "";
        this.country = "";
        this.country_code = "";
    }

}
ClientLocation.Location = function() {
    /// <field name="latitude" type="Number" />
    /// <field name="longitude" type="Number" />
    /// <field name="address" type="ClientLocation.Address" />
    if (arguments.length > 0) {

        this.latitude = arguments[0].latitude;
        this.longitude = arguments[0].longitude;
        this.address = arguments[0].address;

    }
    else {
        this.latitude = 0;
        this.longitude = 0;
        this.address = undefined;
    }

}


// <copyright file="localSearc.js" company="Sky Sanders">
// This source is placed in the Public Domain.
// http://skysanders.net/subtext
// Attribution is appreciated.
// </copyright>
/*
GlocalSearch result

{
"GsearchResultClass": "GlocalSearch",
"viewportmode": "computed",
"listingType": "local",
"lat": "33.389689",
"lng": "-111.853909",
"accuracy": "8",
"title": "Best \u003cb\u003eBuy\u003c/b\u003e",
"titleNoFormatting": "Best Buy",
"ddUrl": "http://www.google.com/maps....",
"ddUrlToHere": "http://www.google.com/maps?....",
"ddUrlFromHere": "http://www.google.com/maps?....",
"streetAddress": "1337 South Alma School Road",
"city": "Mesa",
"region": "AZ",
"country": "United States",
"staticMapUrl": "http://mt.google.com/mapdata?....",
"url": "http://www.google.com/maps/place?source....",
"content": "",
"maxAge": 604800,
"phoneNumbers": [{
"type": "",
"number": "(480) 644-7139"
},
{
"type": "",
"number": "(480) 464-0444"
}],
"addressLines": ["1337 South Alma School Road", "Mesa, AZ"]
}

*/


var LocalSearch = {};

LocalSearch.PhoneNumber = function() {
    /// <field name="type" type="String"/>
    /// <field name="number" type="String"/>
    /// <returns type="LocalSearch.PhoneNumber"/>

    if (arguments.length > 0) {
        this.type = arguments[0].type;
        this.number = arguments[0].number;
    }
    else {
        this.type = "";
        this.number = "";
    }
}



LocalSearch.Result = function() {
    /// <field name="GsearchResultClass" type="String"/>
    /// <field name="viewportmode" type="String"/>
    /// <field name="listingType" type="String"/>
    /// <field name="lat" type="String"/>
    /// <field name="lng" type="String"/>
    /// <field name="accuracy" type="String"/>
    /// <field name="title" type="String"/>
    /// <field name="titleNoFormatting" type="String"/>
    /// <field name="ddUrl" type="String"/>
    /// <field name="ddUrlToHere" type="String"/>
    /// <field name="ddUrlFromHere" type="String"/>
    /// <field name="streetAddress" type="String"/>
    /// <field name="city" type="String"/>
    /// <field name="region" type="String"/>
    /// <field name="country" type="String"/>
    /// <field name="staticMapUrl" type="String"/>
    /// <field name="url" type="String"/>
    /// <field name="content" type="String"/>
    /// <field name="maxAge" type="Number"/>
    /// <field name="phoneNumbers" type="Array"/>
    /// <field name="addressLines" type="Array"/>
    // <returns type="LocalSearch.Result"/>
    if (arguments.length > 0) {
        this.GsearchResultClass = arguments[0].GsearchResultClass;
        this.viewportmode = arguments[0].viewportmode;
        this.listingType = arguments[0].listingType;
        this.lat = arguments[0].lat;
        this.lng = arguments[0].lng;
        this.accuracy = arguments[0].accuracy;
        this.title = arguments[0].title;
        this.titleNoFormatting = arguments[0].titleNoFormatting;
        this.ddUrl = arguments[0].ddUrl;
        this.ddUrlToHere = arguments[0].ddUrlToHere;
        this.ddUrlFromHere = arguments[0].ddUrlFromHere;
        this.streetAddress = arguments[0].streetAddress;
        this.city = arguments[0].city;
        this.region = arguments[0].region;
        this.country = arguments[0].country;
        this.staticMapUrl = arguments[0].staticMapUrl;
        this.url = arguments[0].url;
        this.content = arguments[0].content;
        this.maxAge = arguments[0].maxAge;
        this.phoneNumbers = arguments[0].phoneNumbers;
        this.addressLines = arguments[0].addressLines;

    }
    else {

        this.GsearchResultClass = "";
        this.viewportmode = "";
        this.listingType = "";
        this.lat = "";
        this.lng = "";
        this.accuracy = "";
        this.title = "";
        this.titleNoFormatting = "";
        this.ddUrl = "";
        this.ddUrlToHere = "";
        this.ddUrlFromHere = "";
        this.streetAddress = "";
        this.city = "";
        this.region = "";
        this.country = "";
        this.staticMapUrl = "";
        this.url = "";
        this.content = "";
        this.maxAge = 0;
        this.phoneNumbers = [];
        this.addressLines = [];
    }


}

1
感谢您提供的示例代码Sanders。 我基本上是在寻找一种方法,让用户指定商家名称和城市 - 并使用该信息,我可以使用Google代码获取地址。 Roland向我提供了这个链接,正好可以做到这一点: http://code.google.com/apis/ajax/playground/#center_localsearch 但我的担忧是我是否可以使用那些信息并将其存储在我的数据库中。 - Gublooo
1
是的,我正在完成一个从GLocalSearch中使用简单的JsonP策略获取数据的示例。至于这个示例的存储和使用,您需要查看发布在Google API页面上的使用条款。我发布了这个示例,以便其他寻找此类内容的人可以使用。 - Sky Sanders
1
你需要检查一下Google的许可证。我不认为你可以“擦掉”他们的数据以便重新使用或存储。对于我们的地图绘制,我们必须获得他们的商业许可证。此外,在免费版本中,你所能发起的调用次数是有限制的,但这可能不会影响到你。 - KeyOfJ

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接