jquery與google map api結(jié)合使用 控件,監(jiān)聽器
更新時間:2010年03月04日 18:37:44 作者:
關(guān)于jquery的獲取不再此處累贅,網(wǎng)上有許多關(guān)于jquery的介紹。
Google Maps JavaScript. API可以讓您在自己的網(wǎng)頁上使用Google地圖.在使用API之前,您應(yīng)該先申請一
個API key,申請API key請到:http://code.google.com/apis/maps/signup.html。這里假設(shè)你獲取到的key是:ABQIAA。
關(guān)于jquery的獲取不再此處累贅,網(wǎng)上有許多關(guān)于jquery的介紹。
接著我們就使用JQuery和Google Maps JavaScript. API來結(jié)合表現(xiàn)一下google map的有趣的地圖效果,進而達到熟悉Google Maps JavaScript. API的目標。
先來個HelloChina:
(1)在html文件中編寫html代碼:
map.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta. http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps 與 JQuery結(jié)合使用</title>
<script. src="http://maps.google.com/maps?file=api&v=2&key=ABQIAA" type="text/javascript"></script>
<script. type="text/javascript" src="jquery.js"></script>
<script. type="text/javascript" src="map.js"></script>
</head>
<body>
<div id="map" style="top:100px;left:300px;width: 600px; height: 400px"></div>
<div id="message"></div>
</body>
</html>
(2)在js文件中編寫js代碼
map.js
$(document).ready(function()
{
//檢查瀏覽器兼容性
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(36.94, 106.08), 4);//中國的經(jīng)緯度以及地方放大倍數(shù)
map.setMapType(G_SATELLITE_MAP);
//document卸載時觸發(fā)
$(window).unload(function (){
$('.').unbind();
GUnload();
});
}else
{
alert('你使用的瀏覽器不支持 Google Map!');
}
});
(3)在地址欄輸入頁面對應(yīng)的地址(確定key是和你輸入地址或域名匹配的),查看效果圖,可以看到中國位于地圖的中央。
HolloChina的效果圖
地圖的移動和變換
(1)修改javascript代碼如下:
map.js
$(document).ready(function()
{
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(36.94, 106.08), 4);
//4秒后移動
window.setTimeout(function() {
map.panTo(new GLatLng(35.746512259918504,78.90625));
}, 4000);
$(window).unload(function (){
$('.').unbind();
GUnload();
});
}else
{
alert('你使用的瀏覽器不支持 Google Map!');
}
});
(2)輸入對應(yīng)的地址查看,等上4秒鐘,就可以看到地圖的中心移動到中國的西部(大概的位置):
地圖中心移動到中國的西部
添加控件并修改地圖類型
修改javascript代碼如下:
map.js
$(document).ready(function()
{
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
//小型伸縮控制器
map.addControl(new GSmallMapControl());
//地圖類型控制器
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(36.94,106.08),4);
//將地圖設(shè)置為衛(wèi)星地圖
map.setMapType(G_SATELLITE_MAP);//修改地圖類型
$(window).unload(function (){
$('.').unbind();
GUnload();
});
}else
{
alert('你使用的瀏覽器不支持 Google Map!');
}
});
刷新頁面,看到的效果是衛(wèi)星地圖的左上角有一個小的伸縮控件,右上角是地圖選擇的控件
加入控件后的效果圖
添加事件監(jiān)聽器并開啟滾輪伸縮效果
修改javascript代碼:
map.js
$(document).ready(function()
{
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
//開啟滾輪伸縮效果--鼠標滾輪向前滾放大地圖,反之縮小
map.enableScrollWheelZoom();
//添加moveend事件監(jiān)聽器
GEvent.addListener(map, "moveend", function() {
var center = map.getCenter();
//在這個DIV中顯示地圖中心的經(jīng)緯度
$('#message').text(center.toString());
});
map.setCenter(new GLatLng(36.94,106.08),4);
$(window).unload(function (){
$('.').unbind();
GUnload();
});
}else
{
alert('你使用的瀏覽器不支持 Google Map!');
}
});
此時的地圖在滾動滾輪的時候會發(fā)生伸縮,而拖動完地圖后,地圖左側(cè)的坐標信息會跟著變。
個API key,申請API key請到:http://code.google.com/apis/maps/signup.html。這里假設(shè)你獲取到的key是:ABQIAA。
關(guān)于jquery的獲取不再此處累贅,網(wǎng)上有許多關(guān)于jquery的介紹。
接著我們就使用JQuery和Google Maps JavaScript. API來結(jié)合表現(xiàn)一下google map的有趣的地圖效果,進而達到熟悉Google Maps JavaScript. API的目標。
先來個HelloChina:
(1)在html文件中編寫html代碼:
map.html
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta. http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps 與 JQuery結(jié)合使用</title>
<script. src="http://maps.google.com/maps?file=api&v=2&key=ABQIAA" type="text/javascript"></script>
<script. type="text/javascript" src="jquery.js"></script>
<script. type="text/javascript" src="map.js"></script>
</head>
<body>
<div id="map" style="top:100px;left:300px;width: 600px; height: 400px"></div>
<div id="message"></div>
</body>
</html>
(2)在js文件中編寫js代碼
map.js
復(fù)制代碼 代碼如下:
$(document).ready(function()
{
//檢查瀏覽器兼容性
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(36.94, 106.08), 4);//中國的經(jīng)緯度以及地方放大倍數(shù)
map.setMapType(G_SATELLITE_MAP);
//document卸載時觸發(fā)
$(window).unload(function (){
$('.').unbind();
GUnload();
});
}else
{
alert('你使用的瀏覽器不支持 Google Map!');
}
});
(3)在地址欄輸入頁面對應(yīng)的地址(確定key是和你輸入地址或域名匹配的),查看效果圖,可以看到中國位于地圖的中央。
HolloChina的效果圖
地圖的移動和變換
(1)修改javascript代碼如下:
map.js
復(fù)制代碼 代碼如下:
$(document).ready(function()
{
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(36.94, 106.08), 4);
//4秒后移動
window.setTimeout(function() {
map.panTo(new GLatLng(35.746512259918504,78.90625));
}, 4000);
$(window).unload(function (){
$('.').unbind();
GUnload();
});
}else
{
alert('你使用的瀏覽器不支持 Google Map!');
}
});
(2)輸入對應(yīng)的地址查看,等上4秒鐘,就可以看到地圖的中心移動到中國的西部(大概的位置):
地圖中心移動到中國的西部
添加控件并修改地圖類型
修改javascript代碼如下:
map.js
復(fù)制代碼 代碼如下:
$(document).ready(function()
{
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
//小型伸縮控制器
map.addControl(new GSmallMapControl());
//地圖類型控制器
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(36.94,106.08),4);
//將地圖設(shè)置為衛(wèi)星地圖
map.setMapType(G_SATELLITE_MAP);//修改地圖類型
$(window).unload(function (){
$('.').unbind();
GUnload();
});
}else
{
alert('你使用的瀏覽器不支持 Google Map!');
}
});
刷新頁面,看到的效果是衛(wèi)星地圖的左上角有一個小的伸縮控件,右上角是地圖選擇的控件
加入控件后的效果圖
添加事件監(jiān)聽器并開啟滾輪伸縮效果
修改javascript代碼:
map.js
復(fù)制代碼 代碼如下:
$(document).ready(function()
{
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
//開啟滾輪伸縮效果--鼠標滾輪向前滾放大地圖,反之縮小
map.enableScrollWheelZoom();
//添加moveend事件監(jiān)聽器
GEvent.addListener(map, "moveend", function() {
var center = map.getCenter();
//在這個DIV中顯示地圖中心的經(jīng)緯度
$('#message').text(center.toString());
});
map.setCenter(new GLatLng(36.94,106.08),4);
$(window).unload(function (){
$('.').unbind();
GUnload();
});
}else
{
alert('你使用的瀏覽器不支持 Google Map!');
}
});
此時的地圖在滾動滾輪的時候會發(fā)生伸縮,而拖動完地圖后,地圖左側(cè)的坐標信息會跟著變。
相關(guān)文章
jQuery中closest和parents的區(qū)別分析
本文給大家介紹jquery中parents()和closest()用法與區(qū)別介紹,在jquery中parents()查找父級元素刪除的時候,發(fā)現(xiàn)它不包含根元素,于是用了closest(),效果不錯,下面我來給大家具體的介紹一下2015-05-05jQuery實現(xiàn)form表單基于ajax無刷新提交方法實例代碼
在本篇文章里小編給大家整理了關(guān)于jQuery實現(xiàn)form表單基于ajax無刷新提交方法實例代碼,需要的朋友們參考下。2019-11-11jquery.lazyload 實現(xiàn)圖片延遲加載jquery插件
看到了淘寶產(chǎn)品介紹中,圖片是在下拉滾動條時加載,這是一個很不錯的用戶體驗。減少了頁面加載的時間了,也減輕了服務(wù)器的壓力,就查了下用JQuery..2010-02-02幾種二級聯(lián)動案例(jQuery\Array\Ajax php)
這篇文章主要為大家詳細介紹了幾種二級聯(lián)動案例(jQuery\Array\Ajax php),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08使用jquery的ajax需要注意的地方dataType的設(shè)置
沒有設(shè)置dataType:'json',就出現(xiàn)無法解析返回的data數(shù)據(jù),會把data當作字符串處理,而不是json對象,記得以前是不用設(shè)dataType的,很奇怪,不知道是不是跟jquery版本有關(guān)系2013-08-08