我当前用于加载Google地图脚本的解决方案是一种过时的方式。
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
但这需要花费很长时间,并且渲染内容会延迟。然后,我查看了Google Map Documentation,发现了如何异步加载Goole Map javascript。
所以我在已经使用的javascript中对此进行了测试。这只是我脚本的片段。
jQuery(document).ready(function() {
googleMaploadScript();
someFunction();
}
// Script for loading googlemap with callback to initialize Google map
function googleMaploadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.google.com/maps/api/js?sensor=true&callback=initGoogleMap";
document.body.appendChild(script);
}
// Some function that calls populateGeoMap()
function someFunction() {
(...)
populateGeoMap();
}
// Script for populating google map with locations
function populateGeoMap() {
// This is where I initialized google map each time I load the page using google map
// But since I'm using a callback, I've commented this out.
//initGoogleMap();
var lat = '12.142123';
var lng = '54.524522';
latLng = new google.maps.LatLng(lat,lng); <-- THIS FAILS
}
// Google map init
function initGoogleMap() {
alert('test'); <-- THIS ALERT IS NEVER TRIGGERED
options = {zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP }
map = new google.maps.Map(document.getElementById("map_canvas"), options);
geocoder = new google.maps.Geocoder();
icon = new google.maps.MarkerImage("http://mysite.com/img/pin_red.png", null, null, null, new google.maps.Size(32, 32));
bounds = new google.maps.LatLngBounds();
}
我的脚本在new google.maps.LatLng(lat,lng);
处失败,这是因为initGoogleMap()
尚未运行。
看来script.src
中的回调无效-因为我的警报从未触发。或可能是因为未按正确的顺序整理内容,但仍然应该触发警报。
有人对此有经验吗?
参考方案
我有同样的问题,并这样解决:
问题是您无法从jQuery外部访问jQuery对象本身。
最后的API回调属性只能访问全局javascript。阅读opn javascript名称空间以更好地理解。
我的解决方案是在$ document.ready(function(){...
var global = {};
接下来,您将jQuery块内的init函数作为变量写入地图,并将其附加到全局对象:
global.initMap = function(){ ...}
现在,您可以像这样在url查询字符串中将jquery函数作为全局变量引用:
...&callback=global.initMap
这对我有用。
Google Maps-将地址转换为纬度和经度-PHP后端? - php是否可以转换(在PHP后端):<?php $Address = "Street 1, City, Country"; // just normal address ?> 至<?php $LatLng = "10.0,20.0"; // latitude & lonitude ?> 我当前…
PHP getallheaders替代 - php我正在尝试从服务器上的apache切换到nginx。唯一的问题是我在PHP脚本中使用的getallheaders()函数,该函数不适用于Nginx。我已经尝试过用户在getallheaders函数上的php站点上提供的注释,但这并不返回所有请求标头。请告诉我如何解决这个问题。我真的想切换到Nginx。 参考方案 您仍然可以使用它,但是您必须像这里一样重新定义…
php Singleton类实例将在多个会话中保留吗? - php举一个简单的例子,如果我想计算一个不使用磁盘存储的脚本的命中次数,我可以使用静态类成员来执行此操作吗?用户1:<?php $test = Example::singleton(); $test->visits++; ?> 用户2:<?php $test = Example::singleton(); $test->visits+…
PHP:将字符串拆分为字母和数字部分的最佳方法 - php我有几个格式的字符串AA11 AAAAAA1111111 AA1111111 分离字符串的字母和数字部分的最佳方法(最有效)? 参考方案 如果它们都是一系列字母,然后是一系列数字,并且没有非字母数字字符,那么sscanf()可能比regexp更有效$example = 'AAA11111'; list($alpha,$numeric) =…
php-casperjs获取内部文本 - php我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…