动态加载Google Maps API

麻烦

我想动态加载google maps api-当当前页面上存在特定元素时。

当我尝试使用地址解析器时,收到以下消息:

ReferenceError: google is not defined

因此,我认为无法成功加载maps api,但在萤火虫中一切正常(脚本称为正确)

if ($('.vcard').length > 0) {

    // load google maps when vcard is present

    var s = document.createElement("script");
    s.type = "text/javascript";
    s.src = "https://maps.google.com/maps/api/js?sensor=false";
    $("head").append(s);

    // error here - unable to use google maps
    var geocoder = new google.maps.Geocoder();

有任何想法吗?谢谢你。

麻烦

感谢您指出正确的方向。已经发现,谷歌有一个异步加载的例子:

function loadScript() {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp' +
      '&signed_in=true&callback=initialize';
  document.body.appendChild(script);
}

function addGoogleMap() {

    if ($('.vcard').length > 0) {
        loadScript();
    }
}

function initialize() {

    // get the adress
    var CurrentAddress = $('.adr').children().text();

    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': CurrentAddress }, function (results, status) {

    if (status == google.maps.GeocoderStatus.OK) { 

        console.log('success'); 

    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

动态加载Google Maps API

来自分类Dev

Google Maps API:动态加载标记

来自分类Dev

使用 AngularJS 动态加载 Google Maps API

来自分类Dev

异步加载Google Maps API

来自分类Dev

如何动态填充heatMapData数组并使用Google Maps API加载热图图层?

来自分类Dev

动态设置GMLib的Google Maps Api密钥

来自分类Dev

在Google Maps API上重新加载标记

来自分类Dev

在Google Maps API上重新加载标记

来自分类Dev

Google Maps API无法正确加载

来自分类Dev

Google Maps API:加载特定版本

来自分类Dev

Google MAPS JS API 未加载

来自分类Dev

Google Maps API RefererNotAllowedError

来自分类Dev

Google Maps API DeletedApiProjectMapError

来自分类Dev

Google Maps Api许可

来自分类Dev

Google Maps Api

来自分类Dev

Google Maps REST API

来自分类Dev

Google Maps API请求

来自分类Dev

Google Maps Directions API

来自分类Dev

Google Maps API参数

来自分类Dev

Google Maps API V3-动态添加控件

来自分类Dev

如何使用Google Maps API动态删除标记?

来自分类Dev

Google Maps JavaScript API:动态设置KML层样式

来自分类Dev

动态更改iOS版Google Maps API的响应语言

来自分类Dev

使用Android版Google Maps API实时动态跟踪路线

来自分类Dev

动态图片+地理位置(Google Maps API?)

来自分类Dev

动态设置Google Maps Api Key for GMLib组件

来自分类Dev

Google Maps API v2 Android中的动态缩放

来自分类Dev

Google Maps Api 动态获取经纬度

来自分类Dev

如何以角度重新加载视图。Google Maps API