[英]How do i get coords from MySQL db and pin markers on google maps api
This is the code I have, I can console.log the coords from db but I can't pin markers on index.html page, why?.这是我拥有的代码,我可以 console.log 来自 db 的坐标,但我不能在 index.html 页面上固定标记,为什么? Is it possible to return the "markers" array to initMap function and use it from there, because if i use some sample array in initMap function it pins successfully markers on the map.
是否可以将“标记”数组返回到 initMap function 并从那里使用它,因为如果我在 initMap function 中使用一些样本数组,它会成功地将标记固定在 Z1D78DC8ED51214E518B5114E518B5114 But I never managed how to pin those markers with the coords I get from MySQL db.
但我从来没有设法用我从 MySQL db 获得的坐标来固定这些标记。 Please help
请帮忙
var express = require('express');
var mysql = require('mysql');
var app = express();
var markers=[];
var coords;
var connnection = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "sampleDB"
});
connnection.connect(function(error) {
if (!!error){
console.log('Error');
}
else{
console.log('Connected');
}
});
app.get('/', function(req, resp){
//about mysql
connnection.query("SELECT * FROM mySampleCoords", function (error, rows, fields) {
if (!!error){
console.log('Error in the query');
}
else{
console.log("SUCCESS");
//console.log(rows[0]);
rows.forEach(function(entry, index) {
var coordonate = {
coords:{lat:entry.lat, lng:entry.lng}
}
markers.push(coordonate);
});
console.log(markers);
//for(var i=0;i<markers.length;i++){
//console.log(i, markers[i]);}
/*
for(var i = 0;i < markers.length;i++){
// Add marker
addMarker(markers[i]);
}
// Add Marker Function
function addMarker(props){
var marker = new google.maps.Marker({
position:props.coords,
map:map,
//icon:props.iconImage
});
// Check for customicon
if(props.iconImage){
// Set icon image
marker.setIcon(props.iconImage);
}
// Check content
if(props.content){
var infoWindow = new google.maps.InfoWindow({
content:props.content
});
marker.addListener('click', function(){
infoWindow.open(map, marker);
});
}
}
//end of fct addMarker
*/
//console.log(markers);
//console.log(markers.length);
resp.send(markers);
}//else
});//conn.query
}) //app.get
app.listen(1337);
//------------------------------------------------------------------------------
function initMap(){
// Map options
var options = {
zoom:2.35,
center:{lat:45.2890,lng:21.8877}
}
// New map
var map = new google.maps.Map(document.getElementById('map'), options);
var heatmap = new google.maps.visualization.HeatmapLayer({
data: getPoints(),
radius: 20,
map: map
});
console.log("mesan din initMap");
for(var i = 0;i < markers.length;i++){
// Add marker
addMarker(markers[i]);
}
// Add Marker Function
function addMarker(props){
var marker = new google.maps.Marker({
position:props.coords,
map:map,
//icon:props.iconImage
});
// Check for customicon
if(props.iconImage){
// Set icon image
marker.setIcon(props.iconImage);
}
// Check content
if(props.content){
var infoWindow = new google.maps.InfoWindow({
content:props.content
});
marker.addListener('click', function(){
infoWindow.open(map, marker);
});
}
} //end of fct addMarker
}
You're sending the coordinates in the wrong format.您以错误的格式发送坐标。 You should change this
你应该改变这个
var coordonate = {
coords:{lat:entry.lat, lng:entry.lng}
}
to this对此
var coordonate = new google.maps.LatLng(entry.lat, entry.lng);
You need to call initMap something like this你需要像这样调用 initMap
<script src="https://maps.googleapis.com/maps/api/js?key=[YOURKEY]&libraries=places&callback=initMap"
async defer></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.