日期:2014-05-16  浏览次数:20359 次

预览文章: 初学 Google Maps JavaScript (4) 反向地址解析
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no;" />
<meta charset="utf-8" />
<script src="http://maps.google.com/maps/api/js?sensor=false&language=zh"></script><!--载入 Google Maps-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script><!--载入 jQuery-->
<style>
#setType{-webkit-border-radius:10px;border:1px solid #CCC;padding:5px;background:#FFF;cursor:pointer;}
body{font-size:12px;}
</style>
<script>
var marker=null;
var geocoder;
$(function(){
	$.latLng = function(lat,lng){
		return new google.maps.LatLng(lat,lng);
	}
	$.createMarker = function(opts){
		return new google.maps.Marker(opts);
	}
	$.createInfoWindow = function(opts){
		return new google.maps.InfoWindow(opts);
	}
	$.geocoder = function(){
		return new google.maps.Geocoder();
	}
	$.size = function(w,h){
		return new google.maps.Size(w,h);
	}
	$.moreMarker = function(latLng){
		addRess = "";
		var marker = $.createMarker({position:latLng,map:map,draggable:false});//创建标志
		//event.latLng.toString()
		var infoWindow = $.createInfoWindow({content:'<b>Google Maps</b>',size:$.size(50,50)});
		if (geocoder) {
    	  geocoder.geocode({'latLng': latLng}, function(results, status) {
     	   if (status == google.maps.GeocoderStatus.OK) {
			infoWindow.setContent( results[0].formatted_address );
        } else {
          	alert(status);
        }
      });
    }
		google.maps.event.addListener(marker,"mouseover",function(){// 在标志上创建事件
			infoWindow.open(map,marker);
		});
		google.maps.event.addListener(marker,"mouseout",function(){// 在标志上创建事件
			infoWindow.close(map,marker);
		});
		map.setCenter(latLng);//设置地图中心
	}
	geocoder = $.geocoder();
	var map = new google.maps.Map($("#map")[0],{
		zoom:10,
		center:$.latLng(34.28,117.11),
		mapTypeId:google.maps.MapTypeId.ROADMAP
	});
	var num = 1;
	var mapTypeArr = [
						google.maps.MapTypeId.ROADMAP,
						google.maps.MapTypeId.HYBRID,
						google.maps.MapTypeId.TERRAIN,
						google.maps.MapTypeId.SATELLITE
					];
	$("#setType").mousedown(function(){
		$(this).css('background','#39F');
	}).mouseup(function(){
		$(this).css('background','#FFF');
		num==mapTypeArr.length?num=0:num;
		map.setMapTypeId(mapTypeArr[num]);
		++num;	
	});
	marker = $.createMarker({position:$.latLng(34.290708458919785,117.2353128051758),map:map,title:'煤机东村'});
	google.maps.event.addListener(map,"click",function(event){
		$.moreMarker(event.latLng);
	});
});
</script>
</head>
<body>
<input  type="button"  id="setType" value="切换地图类型">
  	<div id="map" style="width:100%;height:100%;"></div>
</body>
</html>