简体   繁体   English

我如何从 MySQL db 和谷歌地图上的 pin 标记 api 获取坐标

[英]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.

相关问题 如何使用 Google Maps API 自定义不同的标记 CSS? - How do I customize different markers CSS with Google Maps API? 如何在Google Maps API中添加多个叠加标记 - How do I add multiple overlay markers in google maps api 如何在点击时显示标记并使用 google-maps-react API 获取坐标 - How do I display markers on click and get coordinates using google-maps-react API 如何将数据从DB(mysql)加载到google maps API(laravel) - How to load data from a DB(mysql) into the google maps API(laravel) 如何从Google Maps v3 API中删除带有mysql数据的标记? - how to remove markers with mysql data from Google Maps v3 API? 如何在Google Maps Pin上放置文本标签? - How do I put a text label on a google maps pin? 我们如何在Google Maps API中删除绑定到标记的标签 - How do we remove labels bound to markers in google maps api 如何向多个标记添加相同的事件侦听器,然后区分 Google Maps API v3 中侦听器中的标记? - How do I add same event listener to many markers and then differentiate between the markers in the listeners in Google Maps API v3? Google Maps API:如何使用户添加的标记成为永久标记? - Google Maps API: how do I make user-added markers permanent? 如何使标记可拖动,标记和删除? 使用Google Maps JavaScript API - How do I make markers draggable, labeled, and deletable? with Google maps javascript API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM