gpt4 book ai didi

javascript - 如何在 htlm 和 java 脚本中使用从 firebase 数据库检索的纬度和经度在 map 上显示标记

转载 作者:行者123 更新时间:2023-11-28 04:08:10 24 4
gpt4 key购买 nike

我正在从 Firebase 数据库中检索纬度和经度。这些值存储在变量中,即纬度和经度。但是,当我尝试使用这些值在 map 上设置标记时,它不起作用,但是当我给出静态纬度和位置时,标记就会出现。请帮帮我。谢谢

这是我用来检索位置并将其显示在 map 上的 html 文件的代码。`

      <head>
<title>Data From Firebase Database</title>
<!--<meta http-equiv="refresh" content="5" > -->
<style>
#map{

height: 400px;
width: 100%;
}
</style>
</head>
<body>


<div class= ""mainDiv" align= "left">
<h1> Locations </h1>
<table>
<thead>
<tr>
<td>Latitude</td>
<td>Longitude</td>
</tr>
</thead>
<tbody id="table_body">
</tbody>
</table>
</div>

<script src= "https://www.gstatic.com/firebasejs/3.3.2/firebase.js"></script>
<script>

var config = {
apiKey: "AIzaSyCU7LWliFjX7iWtNkZHhuZI0jvih6rffFI",
authDomain: "test26sep-eae46.firebaseio.com",
databaseURL: "https://test26sep-eae46.firebaseio.com/",
storageBucket: "test26sep-eae46.appspot.com",
};
firebase.initializeApp(config);
</script>

<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script>
var rootRef = firebase.database().ref();

rootRef.on("child_added", snap =>{

var latitude = snap.child("latitude").val();
var longitude = snap.child("longitude").val();
initMap(latitude,longitude);

$("#table_body").append("<tr><td>" + latitude + "</td><td>" + longitude + "</td></tr>");

});

</script>

<h1>Map</h1>
<div id="map"></div>

<script>

function initMap(latitude,longitude){


//alert('Values have been gathered click ok to show map !! Welcome Taj , from Arslan !! :PP');
var options = {
zoom: 8,
center: {lat: 31.5546,lng:74.3572}
}
var map = new google.maps.Map(document.getElementById('map'), options);



//show an alert to check if the values are being fetched from firebase database stored in latitude and longitude.
alert (latitude + " " + longitude);


var marker = new google.maps.Marker({
position:{lat: latitude, lng: longitude},
map:map,
});

var infoWindow = new google.maps.InfoWindow({
content:'<h4>Lahore</h4>'
});

marker.addListener('click', function(){
infoWindow.open(map, marker);
});

}
</script>

<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD0w2dY7OZvF6nBvnLLxzAzXi05l_8jc-o">


</script>
</body>
</html>

`

最佳答案

那么你在哪里:

var latitude = snap.child("latitude").val();
var longitude = snap.child("longitude").val();

这些值从 .val() 作为字符串返回。您必须将它们转换为数字才能与 Google map 配合使用。

就这样做:

initMap(parseFloat(latitude), parseFloat(longitude)); 

或者,您将它们与 Maps API 一起使用,例如

position:{lat: parseFloat(latitude), lng: parseFloat(longitude)}

关于javascript - 如何在 htlm 和 java 脚本中使用从 firebase 数据库检索的纬度和经度在 map 上显示标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46522498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com