2010-02-16 33 views
13

我有一個要求,我希望Google Maps API能夠有一個解決方案。我從來沒有使用Google地圖API--所以我對此很新。使用Google Maps API獲取業務地址

在網站首頁有一個形式,當用戶來了,我想下面的事情發生:

1)市域應基於IP

2用戶城市來填充)還有第二個字段稱爲商店名稱 - 當用戶開始輸入商店名稱時 - 我想要獲取該城市中具有該名稱的所有商戶列表並將其顯示爲下拉菜單,用戶可以從中選擇適當的分支。沒有必要在地圖上顯示任何內容。 EGS - 如果在休斯敦一個用戶開始輸入麥當勞,下面的商家列表應該開始顯示出來

  • 麥當勞,12皮爾蘭大道,休斯頓
  • 麥當勞,2600巴里區大道,休斯頓
  • 麥當勞, 262克利爾萊克大道,休斯頓

此外,當我們得到的地址從谷歌API企業上市 - 我們把它作爲一個字符串,我們需要分析它還是我們得到它不同領域的街道名稱,城市,州,郵政編碼等

任何信息或示例將非常感謝 謝謝

+0

我加了同時使用客戶端的位置和本地搜索以獲取本地商家到客戶端通過自己和他人引用,上門找這樣的事情一個樣本。 –

回答

14

我不認爲你想要谷歌地圖。首先,使用條款不允許任何其他用途,除了在可公開訪問的網頁上顯示谷歌地圖上的內容之外,其次,還有另一種Google API可以完全滿足您的需求:客戶端位置API:http://code.google.com/apis/ajax/documentation/#ClientLocation

關於「企業」:您將不得不從某處收集數據 - 我不相信Google會爲此提供服務。也許你可以簡單地使用谷歌搜索API和一些邏輯來只找到商家(http://code.google.com/apis/ajaxsearch/

編輯:http://code.google.com/apis/ajaxsearch/samples.html#local-search

+0

+1完美答案 –

+1

Hello Roland 感謝您的迴應 - 當我指的是業務 - 我正在談論已經出現在谷歌本地搜索上的企業。例如,如果你去http://maps.google.co。在/和搜索麥當勞休斯敦 - 它會告訴你以下結果: 2017美國休斯敦主街, 美國德克薩斯州休斯敦808達拉斯街 美國德克薩斯州休斯敦埃爾金街4005號 因此,如果我通過「MCdonalds」和「Houston」,我們無法從Google獲得此信息。 謝謝 – Gublooo

+0

糟糕 - 抱歉,我沒有看到您的編輯記錄 現在看看它。 – Gublooo

3

更新:我的企業,也許你可以在這個樣本來看看這裏是一個使用google clientlocation api和使用jsonp的localsearch的示例。


<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 我基本上是在尋找一種方式,用戶指定企業名稱和城市 - 並使用該信息,我可以使用谷歌代碼拉地址。 Roland爲我提供了以下鏈接: http://code.google.com/apis/ajax/playground/#center_localsearch 但我擔心的是我是否可以使用該信息並將其存儲在我的數據庫中 – Gublooo

+1

是啊,我剛剛用一個簡單的JsonP策略完成了從GLocalSearch中抽取的樣本。至於存儲和此示例的使用情況,您必須檢查發佈在google api頁面上的使用條款。 我發佈了其他人來找這種東西的樣本 –

+1

您需要檢查Google許可證。我不相信你可以「擦洗」他們的數據以便重複使用/存儲。對於我們的地圖繪製,我們必須和他們一起拿到營業執照。另外,您在免費版本上可以撥打的電話數量有限,但這可能不會對您造成影響。 – KeyOfJ

相關問題