简体   繁体   English

谷歌地理编码通过HTTP回调功能?

[英]Google Geocode via HTTP callback function?

I want to use the google geocode via HTTP functionality to translate a city name into longitude and latitude in for my AJAX web application. 我想通过HTTP功能使用谷歌地理编码将城市名称转换为我的AJAX Web应用程序的经度和纬度。

However, it appears that no callback function exists for the HTTP geocoder functionality 但是,似乎HTTP地理编码器功能不存在回调函数

http://code.google.com/apis/maps/documentation/geocoding/index.html http://code.google.com/apis/maps/documentation/geocoding/index.html

Is that true, no callback function exists? 是真的,没有回调函数吗?

Because if that is true, it essentially means that the Google geocode via HTTP api is useless when used with AJAX because JavaScript will throw a crossdomain exception error. 因为如果这是真的,它实质上意味着当与AJAX一起使用时,通过HTTP api的Google地理编码是无用的,因为JavaScript会引发跨域异常错误。

Any ideas on how I can use the geocode via HTTP api in my AJAX web application in JavaScript? 关于如何在JavaScript中的AJAX Web应用程序中通过HTTP api使用地理编码的任何想法?

Note : I do not want to use the full blown Google Maps API which is approx 200kb download (ie GClientGeocoder). :我不想使用完全成熟的谷歌地图API,它是大约200KB的下载(即的GClientGeocoder)。 I want to use the HTTP api b/c of it's super quick responsiveness and lack of needing my web users from having to download the huge full blown interactive google maps api. 我想使用它的超快速响应的HTTP api b / c,并且不需要我的网络用户必须下载巨大的全面交互式谷歌地图api。

Eg http://maps.google.com/maps/geo?output=json&sensor=false&key= {API_KEY}&q={CITY,STATE}&CALLBACK=???? 例如http://maps.google.com/maps/geo?output=json&sensor=false&key= {API_KEY}&q = {CITY,STATE}&CALLBACK = ????

Thanks 谢谢

hmm....I think you'd have to have your AJAX call back to your own server, and then call Google's Geocode from your server. 嗯....我想你必须把你的AJAX电话回到你自己的服务器,然后从你的服务器调用谷歌的地理编码。

Thats how I do AJAX geocoding, it all goes through my ASP.NET code. 多数民众赞成我如何进行AJAX地理编码,这一切都通过我的ASP.NET代码。

EDIT: 编辑:

In the ASP.NET webforms environment I might implements this as a lightweight ASHX file, but for the purposes of simplicity, here's an ASPX example: 在ASP.NET webforms环境中,我可能将其实现为轻量级ASHX文件,但为了简单起见,这是一个ASPX示例:

public partial class GoogleHandler : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {
        Response.Write(GetGoogleXML("http://pseudo_googlegeocode?parameter=" + parametersFromQuerystring);
    }
}

In the example above, the .NET page is only passing the request along. 在上面的示例中,.NET页面仅传递请求。

But in a real environment, I'd rather my .NET code do more than just pass the data over. 但在真实的环境中,我宁愿我的.NET代码做的不仅仅是传递数据。 This way I can do error handling, filtering, validation, business logic, all on the server, before sending that data over to the client. 这样我就可以在将数据发送到客户端之前,在服务器上执行错误处理,过滤,验证,业务逻辑。

Also, this allows for greater abstraction. 而且,这允许更大的抽象。 ie, I might change from google to yahoo geocoding. 即,我可能会从谷歌改为雅虎地理编码。 In this way I'd only have to change my serve logic, and leave the client to just receive a generic set of coordinates/location data. 通过这种方式,我只需要更改我的服务逻辑,并让客户端只接收一组通用的坐标/位置数据。

Also, using that abstraction I could actually aggregate multiple data from various geocoding data sources. 此外,使用该抽象,我实际上可以聚合来自各种地理编码数据源的多个数据。 Again, the server takes care of aggregating, the client just receives and displays the filtered data. 同样,服务器负责聚合,客户端只接收并显示过滤后的数据。

Here is an example that uses the Google Maps Geocoder. 以下是使用Google Maps Geocoder的示例。 The geocoder function getLocation takes a callback function as the second argument. 地理编码器函数getLocation将回调函数作为第二个参数。

function findAddress(street, city, state, zip) {
  var address = [
    street,
    city.toLowerCase(),
    state.toLowerCase(),
    zip
  ].join(', ');

  if (!geocoder) {
    geocoder = new GClientGeocoder();
  }

  if (geocoder) {
    geocoder.getLocations(
      address,
      function(result) {
        var dialog, len, point;
        if (result.Status.code != G_GEO_SUCCESS) {
          alert("Error: "+result.Status.code)
        } else {
          len = result.Placemark.length;
          if (len > 1) {
            alert("Multiple matches were found.  I'll leave it as an exercise to handle this condition");
          } else {
            point = new GLatLng(
              result.Placemark[0].Point.coordinates[1],
              result.Placemark[0].Point.coordinates[0]
            );
          }
        }
      }
    );
  }
}

As others noted, you didn't read the full page. 正如其他人所说,你没有读完整页。 You want what that page calls the JavaScript Client Geocode . 您想要该页面调用JavaScript客户端地理编码

Here's a simplified version of a script I wrote a while back. 这是我之前写过的脚本的简化版本。 It also uses a Google Map control, but feel free to ignore that. 它还使用Google Map控件,但可以随意忽略它。 The delay function hack is because it seemed Google was occasionally returning null when I hit their servers too fast. 延迟函数hack是因为当我点击他们的服务器太快时,Google似乎偶尔会返回null。 I don't know if this is still an issue, so don't put it in unless you have to. 我不知道这是否还是一个问题,所以除非必须,否则不要把它放进去。

<script type="text/javascript">

    //<![CDATA[

    var freezeLocations;
    var coder;
    var map;

    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(38.479395, -98.349609), 4);
        map.addControl(new GLargeMapControl());
      }

      coder = new GClientGeocoder();

      missionLocations = new Array();
      missionLocationsDelayed = new Array();
      addMissionLocation("Atlanta, Georgia", "http://improveverywhere.ning.com/group/atlanta");
      //etc.
    }

    function addMissionLocation(newLocation, url)
    {
        var successful = false;
        var counter = 0;

        while(!successful && counter < 3)
        {
            coder.getLatLng(
                newLocation,
                function(point) {
                    if (!point) {
                        //alert(newLocation + " not found");
                        successful = false;
                    } else {
                        missionLocations.push(new GMarker(point, { title:newLocation}));
                        //alert(missionLocations.length);
                        map.addOverlay(missionLocations[missionLocations.length - 1]);
                        missionLocations[missionLocations.length - 1].bindInfoWindowHtml("<a href='" + url + "'>" + newLocation + "</a>");
                        successful = true;
                    }
                }
            );

            if(!successful)
            {
                delayGeocode();
            }

            counter++;
        }
    }

    function delayGeocode()
    {
        for(var i = 0; i < 2000000; i++)
        {
        }
    }


    //]]>
    </script>

You could use Yahoo Query language as outlined in my blog post http://jawtek.blogspot.com/2009/03/unwritten-guide-to-yahoo-query-langauge.html 您可以使用我的博客文章http://jawtek.blogspot.com/2009/03/unwritten-guide-to-yahoo-query-langauge.html中概述的Yahoo Query语言

You would be able to use a yql statement like: 您将能够使用如下的yql语句:

select * from json where
  url="http://maps.google.com/maps/geo?output=json&sensor=false&q=Indianapolis,In"

Then you would add a script tag to your html (can be done with document.createElement('script')) with a src http://query.yahooapis.com/v1/public/yql?q= {your yql here}&format=json&callback={your function here} where {your yql here} is replace with a URI Encoded version of you yql statment. 然后你将使用src http://query.yahooapis.com/v1/public/yql?q= {your yql here}为你的html添加一个脚本标签(可以使用document.createElement('script'))。 &format = json&callback = {your function here}其中{your yql here}将替换为您的yql statment的URI编码版本。

If you have looked at the documentation and not found it and both Andrew and Mike have not said "yes", and told you how to do it, I suspect you have your answer. 如果您查看了文档并且没有找到它,安德鲁和迈克都没有说“是”,并且告诉您如何操作,我怀疑您有答案。

lol 大声笑

and lets all read the service's documentation: 并让所有人阅读服务的文档:

10.13 hide or mask from Google the identity of your service as it uses the Service, including by failing to follow the identification conventions listed in the Maps APIs Documentation; 10.13在使用本服务时隐藏或屏蔽您的服务标识,包括未遵守Maps API文档中列出的标识约定; or 10.14 violate any policies in the Maps APIs Documentation or violate Google's Software Principles (...) 或10.14违反Maps API文档中的任何政策或违反Google的软件准则(...)

Also

This service is designed for geocoding static (known) addresses using a REST interface, for placement of application content on a map. 此服务旨在使用REST接口对静态(已知)地址进行地理编码,以便在地图上放置应用程序内容。 For dynamic geocoding of user-defined addresses (for example, within a user interface element), consult the documentation for the JavaScript Client Geocoder or the Maps API for Flash Client Geocoder. 对于用户定义地址的动态地理编码(例如,在用户界面元素中),请参阅JavaScript客户端地理编码器或Maps API for Flash Client Geocoder的文档。 Geocoding is a time and resource intensive task. 地理编码是一项耗费时间和资源的任务。 Whenever possible, pre-geocode known addresses (using the Geocoding Service described here or another geocoding service), and store your results in a temporary cache of your own design. 尽可能预先对已知地址进行地理编码(使用此处描述的地理编码服务或其他地理编码服务),并将结果存储在您自己设计的临时缓存中。

But then again you could try Google Maps API V3 Geocoder 但是,您可以再次尝试使用Google Maps API V3 Geocoder

I too encountered the challenges you described above. 我也遇到了你上面描述的挑战。 As you indicated, Google prevents cross-domain HTTP access to the Geocode API URL: 正如您所指出的,Google阻止了对Geocode API网址的跨域HTTP访问:

This does severely diminish its usefulness when using client-side scripting. 使用客户端脚本时,这会严重降低其实用性。 The only solution I found to this challenge was to create a server-side proxy service that relays the responses from the Google Maps Geocode API to my client-side script. 我发现这个挑战的唯一解决方案是创建一个服务器端代理服务,将来自Google Maps Geocode API的响应转发到我的客户端脚本。

I wrote an extremely long-winded blog post describing this process. 我写了一篇非常啰嗦的博客文章描述了这个过程。

Look at the Google Maps API. 查看Google Maps API。 It has some functions with callback that uses it's geocoding service. 它有一些回调功能,使用它的地理编码服务。

http://code.google.com/apis/maps/documentation/reference.html#GClientGeocoder.getLatLng http://code.google.com/apis/maps/documentation/reference.html#GClientGeocoder.getLatLng

I second the suggestion to create a server-side page to access the geocoder. 我建议创建一个服务器端页面来访问地理编码器。 I am doing something similar and it works great. 我正在做类似的事情并且效果很好。 There's a good article about working with the geocoder in PHP here . 有一个关于与地理编码在PHP工作的好文章在这里

Also note that technically you're not permitted to use Google's geocoder unless you'll be displaying the data on a Google Map - but I don't know if they'll actually check on you. 另请注意,从技术上讲除非您要在Google地图上显示数据,否则不允许使用Google的地理编码器 - 但我不知道他们是否真的会检查您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM