iT邦幫忙

1

google map 由mongodb抓取座標方法 使用node

data
  location:{
    type:[Number],
    require:true
  },
location: [
-123,
29
],

這是我現在model的部分
我現在get資料後 前端是能查出-123,29這樣
不過我把它用在googlemap上就不行

  var location = data.location
  var latLng = new google.maps.LatLng(location);

  function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
  center:latLng,
  zoom: 15
  });

地圖都出不來

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

3
dragonH
iT邦超人 5 級 ‧ 2019-09-24 22:27:20
最佳解答

你的 location 是 array 嗎

官方 doc

沒說可以用 array 阿

你 console 應該也會有相關訊息吧

看更多先前的回應...收起先前的回應...

好像是 前端JS取不到值耶
他說我的data未定義
請問前端JS有甚麼方法能取值嗎
之前都只有從html取而已

dragonH iT邦超人 5 級 ‧ 2019-09-24 23:06:23 檢舉

shrine90459.
你是怎麼跟後端要座標資料的

我是router.get(req,res)=>
    Data.find(function(data){
    res.render('',{data:data});
    };

然後前端pug裡面data.location有資料
但script那邊就取不到值

script.
  var map;
  var mylocation=data.location; //這樣似乎不行
dragonH iT邦超人 5 級 ‧ 2019-09-25 00:27:12 檢舉

shrine90459

這個給你參考

所以你的問題應該在於

如何將變數帶到 pug template 的 script 裡面

所以你應該要寫成

var mylocation = #{data.location};

我要發表回答

立即登入回答