日期:2014-05-16 浏览次数:20363 次
<!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>Google 地图 JavaScript API 示例: 简单的地图</title> <style> html,body{height:100%;margin:0;padding:0;} #map_canvas{height:100%;} @media print{ html,body{height:auto;} #map_canvas{height:600px;} } </style> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=zh-CN"></script> <script type="text/javascript"> // map.js start var $G,$O,$M,$L,$I; (function(){ O = function (id) { return "string" == typeof id ? document.getElementById(id):id; }; MP = { y:39.9, x:116.4, point:function(y,x){ return new google.maps.LatLng(y,x); }, getCanvas:function(id){ var mapid = id?id:'map_canvas'; return document.getElementById(mapid); }, options:function(center,z){ return { zoom: z?z:14, center: center?center:this.getCenter(), navigationControl: true, scaleControl: true, streetViewControl: true, mapTypeId: google.maps.MapTypeId.ROADMAP } }, } M = { mark:function(map,latLng,title){ if(title) return new google.maps.Marker({ icon: this.icon, position: latLng, map: map, title:title }); else return new google.maps.Marker({ //icon: this.icon, position: latLng, map: map }); } } I = { infos:[], add:function(info,latLng,w,h){ if(w&&h) return new google.maps.InfoWindow({ content: info, size: new google.maps.Size(w,h), position: latLng }); else if(latLng) return new google.maps.InfoWindow({ content: info, position: latLng }); else return new google.maps.InfoWindow({ content: info }); } } //event 事件 L = { listen:null, add:function(dom,event,fn){ return google.maps.event.addDomListener(dom, event, fn); } } $G = MP; $O = O; $M = M; $L = L; $I = I; })(); // map.js end //以上的js建议抽取出来存为map.js再引入项目使用,以增加通用性 //初始化坐标 $G.y=39.9126328872148; $G.x=116.44053633792112; var inf = "蒼井空はどこ?"; var point = $G.point($G.y,$G.x); var info = $I.add(inf,point); var map; function initialize(){ map = new google.maps.Map($G.getCanvas("map_canvas"), $G.options(point,15)); //初始化地图 var mark = $M.mark(map,point,"ここにいるよう"); info.open(map); $L.listen = $L.add(mark,"click",function(){info.open(map)}); //给标记点添加一个点击事件 } </script> </head> <body onload="initialize()"> <div id="map_canvas"></div> </body> <script> </script> </html>