This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooglemap.html
85 lines (80 loc) · 2.72 KB
/
googlemap.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<html>
<head>
<title>Elaine Wu:Google Maps 使用Geocoder查詢地址經緯度定位地圖並加入Marker</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta charset="utf-8">
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0 20%; padding: 0px }
#map_canvas { height: 100% }
</style>
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="https://googledrive.com/host/0B6dtn0LtYgFgUUFtMzhOWHpyQ0k/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
var map;
var marker;
function initialize()
{
//初始化地圖時的定位經緯度設定
var latlng = new google.maps.LatLng(22.631386,120.30195100000003);
//初始化地圖options設定
var mapOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//初始化地圖
map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
//加入標記點
marker = new google.maps.Marker({
draggable:true,
position: latlng,
title:"美麗島站",
map:map
});
//增加標記點的mouseup事件
google.maps.event.addListener(marker, 'mouseup', function() {
LatLng = marker.getPosition();
$("#NowLatLng").html("【移動標記點後的位置】緯度:" + LatLng.lat() + "經度:" + LatLng.lng());
});
}
function GetAddressMarker()
{//重新定位地圖位置與標記點位置
address = $("#address_val").val();
geocoder = new google.maps.Geocoder();
geocoder.geocode(
{
'address':address
},function (results,status)
{
if(status==google.maps.GeocoderStatus.OK)
{
//console.log(results[0].geometry.location);
LatLng = results[0].geometry.location;
map.setCenter(LatLng); //將地圖中心定位到查詢結果
marker.setPosition(LatLng); //將標記點定位到查詢結果
marker.setTitle(address); //重新設定標記點的title
$("#SearchLatLng").html("【您輸入的地址位置】緯度:" + LatLng.lat() + "經度:" + LatLng.lng());
}
}
);
}
$(document).ready(function() {
//綁定地址輸入框的keyup事件以即時重新定位
$("#address_val").bind("keyup",function(){
GetAddressMarker();
$("#NowLatLng").html("【移動標記點後的位置】");
});
});
</script>
</head>
<body onload="initialize()">
<div style="height:150;">
<div>請輸入地址:<input type="text" id="address_val" name="address_val" style="width:400px;" ></div>
<div id="SearchLatLng">【您輸入的地址位置】</div>
<div id="NowLatLng">【移動標記點後的位置】</div>
</div>
<div id="map_canvas" style="height:80%;"></div>
</body>
</html>