【问题标题】:Geocoding in Google Earth API using Maps V3使用 Maps V3 在 Google Earth API 中进行地理编码
【发布时间】:2013-11-23 04:53:53
【问题描述】:

我不久前使用 GE 插件开发了一个应用程序。在该应用程序中,我使用了当然取决于(地图,2.xx)的地理编码功能。随着 Google Maps V2 的弃用,这部分代码不再有效。我的代码基于 Google 地球演示站点 (http://earth-api-samples.googlecode.com/svn/trunk/examples/geocoder.html) 中的地理编码示例应用程序,该应用程序也不再有效。

我搜索了 v3 站点,但找不到处理此问题的方法。使用 google.load("地图", "3.xx");
代替 google.load("地图", "2.xx"); 根本不起作用,我收到了 Google 地图服务器的拒绝。

【问题讨论】:

    标签: google-maps-api-3 google-earth-plugin


    【解决方案1】:

    使用google.load("maps", "3.xx"); 不起作用的原因是您必须提供传感器参数。即google.load('maps','3.6', { other_params: 'sensor=false' });

    必须包含 URL 的 sensor 参数,并表示 此应用程序是否使用传感器(例如 GPS 定位器)来 确定用户的位置。我们把这个例子作为一个变量 set_to_true_or_false 强调您必须将此值设置为 明确地判断为真或假。

    见:https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API

    无论如何,我创建了一个working example 使用 Google Maps V3 地理编码器和 Earth Api,让您了解它是如何工作的。

    这里还有一个代码示例,它对术语“纽约”进行地理编码,并在插件和地图 api 加载后移至找到的第一个结果(以防 jsfiddle 将来消失......)

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load('earth', '1');
    google.load('maps','3.6', { other_params: 'sensor=false' }); // or true
    
    var ge = null; // GEPlugin
    var geocoder = null; // GClientGeocoder
    
    var init = function() { 
      google.earth.createInstance('map3d', initCallback, failureCallback);
    };
    
    var initCallback = function(object) {
      ge = object;
      geocoder = new window.google.maps.Geocoder(); //v3 Geocoder
      ge.getWindow().setVisibility(true);
    
      // for example: geocode New York
      geocode("New York");
    };
    
    var failureCallback = function(error) {
      alert("Plugin Error: " + error);
    };
    
    var geocode = function(address) {
      geocoder.geocode({ 'address': address }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            // do something with the result, such as flying to it...
            var point = results[0].geometry.location;
            var lookat = ge.createLookAt('');
            lookat.set(point.lat(), point.lng(), 100, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 1000);
            ge.getView().setAbstractView(lookat);
        } else {
            alert("Geocode Error: " + status);
        }
      });
    };
    
    google.setOnLoadCallback(init);
    </script>
    

    【讨论】:

      【解决方案2】:

      您可以加载 v3 脚本并修改项目的地理编码部分,但如果您只需要地理编码,则可以直接致电 Geocoding web service

      或者,您可以保留现有代码:大多数旧 v2 代码实际上仍然有效,只是 GLatLng.x 和 y 是 never supported。因此,分别使用 point.lng() 和 point.lat() 代替 point.x 和 point.y。

      【讨论】:

        猜你喜欢
        • 2013-04-05
        • 1970-01-01
        • 2013-12-18
        • 2013-10-05
        • 1970-01-01
        • 2013-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多