JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<title>移動狀況示範</title>
<!-- 主要程式架構參考自 https://developers.google.com/maps/documentation/javascript/examples/event-arguments?hl=zh-tw -->
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 80%;
}
</style>
<!-- Firebase程式範例參考自 http://practical-dbm.blogspot.tw/search/label/Firebase -->
<script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script>
<script>
// Initialize Firebase
var firebaseConfig = {
apiKey: "專案API金鑰號碼", // 若資料庫為PUBLIC就不需要這行
authDomain: "專案驗證網址", // 若資料庫為PUBLIC就不需要這行
databaseURL: "https://專案資料庫網址",
projectId: "project-id", // 若資料庫為PUBLIC就不需要這行
storageBucket: "專案檔案空間", // 若不使用檔案空間就不需要這行
messagingSenderId: "專案通知傳送序號", // 若不使用通知傳送就不需要這行
appID: "app-id" // 若資料庫為PUBLIC就不需要這行
};
var firebaseConfig = {
databaseURL: "https://ytuytuuytuyt.firebaseio.com",
};
firebase.initializeApp(firebaseConfig);
</script>
</head>
<body>
<div id="map"></div>
<!-- 多增加一個msg區塊用以呈現必要的資訊 -->
<p>請在地圖上點一下,然後去觀察另一支程式是否同步呈現該位置...</p>
<div id="msg"></div>
<script>
//將原範例程式在placeMarkerAndPanTo()中的marker變數改成宣告為整體性變數
var marker;
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 17,
center: {lat: 24.121438, lng: 120.675038 }
});
// Try HTML5 geolocation. 參考 http://www.w3schools.com/html/html5_geolocation.asp
// As of Chrome 5.0, the Geolocation API will only work on secure contexts such as HTTPS.
// If your site is hosted on an non-secure origin (such as HTTP),
// the requests to get the users location will no longer function.
if (navigator.geolocation) {
document.getElementById("msg").innerHTML = "偵測目前座標 <img src='loading9.gif' height='12'...