最近需要用到Google地图,整理下:
官方文档地址:http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/basics.html
html代码:
- <body onload="initialize()">
- <div id="map_canvas"></div>
- <div id="content" style="display:none;" mce_style="display:none;" ><span id="loca"></span><br />
- <span id="addr"></span></div>
JavaScript代码:
- <mce:script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false" mce_src="http://maps.google.com/maps/api/js?sensor=false"></mce:script>
- <mce:script type="text/javascript"><!--
- var map;
- var marker;
- var infowindow;
- var geocoder;
- var markersArray = [];
- var content = document.getElementById("content");
- var loca = document.getElementById("loca")
- var addr = document.getElementById("addr")
- function initialize() {
- geocoder = new google.maps.Geocoder();
- var latlng = new google.maps.LatLng(28.212651557421317,112.94564378840637);
- var myOptions = {
- zoom: 9,
- center: latlng,
- navigationControl: true,
- scaleControl: true,
- streetViewControl: true,
- mapTypeId: google.maps.MapTypeId.ROADMAP
- };
- map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
- google.maps.event.addListener(map, 'click', function(event) {
- placeMarker(event.latLng);
- });
- }
- function placeMarker(location) {
- clearOverlays(infowindow);
- marker = new google.maps.Marker({
- position: location,
- map: map
- });
- markersArray.push(marker);
- var _cs = [];
- _cs[_cs.length] = "坐标为:";
- _cs[_cs.length] = location.lat();
- _cs[_cs.length] = ",";
- _cs[_cs.length] = location.lng();
- loca.innerHTML = _cs.join("");
- if (geocoder) {
- geocoder.geocode({'location': location}, function(results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- if (results[0]) {
- addr.innerHTML = " 地址为:" + results[0].formatted_address;
- }
- } else {
- alert("Geocoder failed due to: " + status);
- }
- });
- }
- content.style.display = "";
- infowindow = new google.maps.InfoWindow({
- content: content,
- //size: new google.maps.Size(50,50),
- position: location
- });
- infowindow.open(map);
- }
- // Deletes all markers in the array by removing references to them
- function clearOverlays(infowindow) {
- if (markersArray) {
- for (i in markersArray) {
- markersArray[i].setMap(null);
- }
- markersArray.length = 0;
- }
- if(infowindow){
- infowindow.close();
- }
- }
- // --></mce:script>
附完整例子下载地址:http://download.csdn.net/source/2868497