-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgMap.html
89 lines (72 loc) · 2.36 KB
/
gMap.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
86
87
88
89
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
//初始化
function initialize() {
//地圖核心位置
var mapProp = {
center:new google.maps.LatLng(25, 121.5),
zoom:12,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
//讀取jsno檔
var xmlhttp = new XMLHttpRequest();
var url = "/assets.json";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
myFunction(myArr);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(arr) {
var out = "";
var i;
var infowindow;
for(i = 0; i < arr.length; i++) {
//地圖底下的呈現
out += '<p>' + arr[i].name + " "+
arr[i].type + " " + arr[i].adress + '</p><br>';
//放上marker
var marker=new google.maps.Marker({
position:new google.maps.LatLng(arr[i].latitude, arr[i].longitude),
});
marker.setMap(map);
infowindow = new google.maps.InfoWindow({
});
var content=arr[i].content;
//監聽點擊事件
google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){
return function() {
if (isInfoWindowOpen(infowindow)){
// do something if it is open
infowindow.close();
} else {
// do something if it is closed
infowindow.setContent(content);
infowindow.open(map,marker);
}
};
})(marker,content,infowindow));
}
document.getElementById("show").innerHTML = out;
}
//判斷是偶有打開資訊欄
function isInfoWindowOpen(infoWindow){
var map = infoWindow.getMap();
return (map !== null && typeof map !== "undefined");
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="googleMap" style="width:600px;height:480px;"></div>
<div id="show"></div>
</body>
</html>