Google地图API方式查看卫星地图

最近发现谷歌地图http://www.google.cn/maps 左下角点击卫星图像的时候,不能切换到卫星地图模式,于是借助Google地图API方式写了个DEMO,算是学习了下Google地图,代码如下:

<!DOCTYPE html>
<html>
<head>
<title>Google Map</title>
<meta charset="UTF-8">
<script type="text/javascript" src="http://ditu.google.cn/maps/api/js?v=3&sensor=false&language=zh-cn"></script>
<script type="text/javascript">
	window.onload = function() {
	var geo = new geoEach();
	// 解析地名到经纬度
	var area = '中国\n南京';
	area = area.split('\n');
	var sh;
	var i = 0;
	//
	sh = window.setInterval(function() {
		if (i < area.length) {
			geo.toLatLng(area[i]);
		} else {
			window.clearInterval(sh);
		}
		i++;
	}, 500);//每隔500毫秒解析地名直至到最小地名后关闭定时器
}
function geoEach() {
	// 初始化地图
	geoEach.map = new google.maps.Map(document.getElementById("map_canvas"), {
		center : new google.maps.LatLng(35.86166, 104.19539699999996),
		zoom : 4,
		mapTypeId : google.maps.MapTypeId.HYBRID
	});
	// 添加一个marker
	geoEach.marker = new google.maps.Marker({
		map : geoEach.map,
		position : new google.maps.LatLng(35.86166, 104.19539699999996)
	});
	// 实例化geocoder服务
	geoEach.geocoder = new google.maps.Geocoder();
}
geoEach.address = null;// 需要解析的地名
geoEach.prototype = {
	// 解析地名
	toLatLng : function(address) {
		// 接收
		geoEach.address = address;
		// 执行geocoder解析地名
		geoEach.geocoder.geocode({
			// 传入地名
			'address' : geoEach.address
		}, function(results, status) {
			// create div
			var newElement = window.document.createElement('p');
			if (status == google.maps.GeocoderStatus.OK) {
				// 获取解析后的经纬度
				var location = results[0].geometry.location;
				// 切换地图位置
				geoEach.map.setCenter(location);
				geoEach.marker.setPosition(location);

				var latLng = location.toString().substr(1,location.toString().indexOf(')') - 1);
				// insert innerHTML
				newElement.innerHTML = geoEach.address + ":" + latLng;
			} else {
				// insert error innerHTML
				newElement.innerHTML = geoEach.address + ":error" + status;
			}
		});
	}
}
</script>
</head>
<body>   
<div id="map_canvas" style="width: 100%; height: 580px"></div>
</body>
</html>
效果图如下:



  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值