te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - google maps api loading map on button click - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - google maps api loading map on button click - Stack Overflow

programmeradmin3浏览0评论

I am trying to initialize the map load on a simple button click but with no luck. Also no errors being thrown in the javascript console.

If I change it to google.maps.event.addDomListener(window, 'load', initialize); it works fine.

var showMap = $('#show-map');

function initialize() {
    var mapOptions = {
	    center: { lat: 0, lng: 0 },
	    zoom: 8
	};
	var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

google.maps.event.addDomListener(showMap, 'click', initialize);
#map-canvas {
  height: 30em;
  width: 30em;
}
<script src=".1.1/jquery.min.js"></script>
<script src=""></script>

<button id="show-map">Show Map</button>

<div id="map-canvas"></div>

I am trying to initialize the map load on a simple button click but with no luck. Also no errors being thrown in the javascript console.

If I change it to google.maps.event.addDomListener(window, 'load', initialize); it works fine.

var showMap = $('#show-map');

function initialize() {
    var mapOptions = {
	    center: { lat: 0, lng: 0 },
	    zoom: 8
	};
	var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

google.maps.event.addDomListener(showMap, 'click', initialize);
#map-canvas {
  height: 30em;
  width: 30em;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis./maps/api/js?key=AIzaSyCgxY6DqJ4TxnRfKjlZR8SfLSQRtOSTxEU"></script>

<button id="show-map">Show Map</button>

<div id="map-canvas"></div>

Share Improve this question edited Jun 22, 2015 at 17:45 duncan 31.9k15 gold badges81 silver badges101 bronze badges asked Jun 22, 2015 at 15:58 Mr.SmithyyyMr.Smithyyy 1,32913 gold badges54 silver badges104 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Instead of using google.maps.event handlers for buttons, use a jQuery event listener. Even though it is possible to use Google maps event handlers for the rest of the DOM, it is better practice to use jQuery for this. Also, I think that Sebastián Rojas made a good suggestion about using window.onload.

Here is the Fiddle. The only change from the original code is:

$(document).ready(function(){
    $('#show-map').on('click',initialize)
});

This code waits until the document is loaded, and then it assigns the event listener to the button (#show-map), to do initialize when it is clicked.

You can't replace the event of initialization, because is important that the window is loaded and Google Maps API also. So, instead, leave the dom listener as documentations suggest, and when the event is fired attach an event listener to the button click to show the map

function initialize() {

    //Add the event listener after Google Mpas and window is loaded
    $('#show-map').click(function () {
         var mapOptions = {
	    center: { lat: 0, lng: 0 },
	    zoom: 8
	};
	var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    });
}

google.maps.event.addDomListener(window, 'load', initialize);
#map-canvas {
  height: 30em;
  width: 30em;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis./maps/api/js?key=AIzaSyCgxY6DqJ4TxnRfKjlZR8SfLSQRtOSTxEU"></script>

<button id="show-map">Show Map</button>

<div id="map-canvas"></div>

As a quick-fix, this will work:

var map = document.getElementById('show-map')
google.maps.event.addDomListener(map, 'click', initialize);
发布评论

评论列表(0)

  1. 暂无评论