2016-11-03 80 views
-1

我創建了一些包含一些鏈接的地圖(點擊時)使用jquery更改地圖位置。試圖在動態地圖中創建多個Google地圖標記

我試圖爲每個位置實現一個標記,但是要讓標記顯示在除默認設置之外的其他任何地圖位置上。

我試圖爲每個緯度和長時間值設置一個標記。我試圖使用下面的代碼,但我得到的錯誤:TypeError:map.setPosition不是螢火蟲控制檯中的函數。

我的代碼如下:

HTML:

<a id="link-aberdeen" href="javascript:void(0)" class="button1">Munich</a>  
<a id="link-glasgow" href="javascript:void(0)" class="button2">Oxford</a>  
<a id="link-glasgow" href="javascript:void(0)" class="button3">Glasgow</a> 

<div id="map-canvas"></div> 

CSS:

#map-canvas { background: #eaeaea none repeat scroll 0 0; height: 300px; width: 100%; } 

JAVASCRIPT:

function initialize() { 

map = new google.maps.Map(document.getElementById('map-canvas'), { 
center: new google.maps.LatLng(48.1293954,12.556663), //Setting Initial Position 
zoom: 10 
}); 

var marker = new google.maps.Marker({ 
    map: map, 
    position: new google.maps.LatLng(48.1293954,12.556663), 
center: new google.maps.LatLng(48.1293954,12.556663), 
zoom: 10 
}); 

} 


function newLocation(newLat,newLng) { 
map.setCenter({ 
    lat : newLat, 
    lng : newLng 
}); 

map.setPosition({ 
    lat : newLat, 
    lng : newLng 
}); 

} 

google.maps.event.addDomListener(window, 'load', initialize); 


$(document).ready(function() { 

$(".button1").on('click', function() { 
    newLocation(48.1293954,12.556663); 
}); 

$(".button2").on('click', function() { 
    newLocation(51.7163950,-1.2196100); 
}); 

$(".button3").on('click', function() { 
    newLocation(55.8610240,-4.2614560); 
}); 

}); 

我也有一個小提琴我一直在測試請參閱https://jsfiddle.net/xxfairydragonxx/7j31df1v/

任何人都可以幫助我嗎?

回答

0

我不知道爲什麼我不能讓這個在小提琴工作...
但它在CodePen

我修好了很多小東西。

  1. 使用initialize,而不是initMap在谷歌API腳本調用回調...
    因爲這就是你如何命名你的函數。
  2. map聲明爲全局(在特定函數之外)。
  3. 所有關於地圖的內容都應該在initialize function之內。
  4. 不要忘記加載jQuery,如果你使用它的事情就像你的3個鏈接!


代碼:

var map; 

function initialize() { 
    map = new google.maps.Map(document.getElementById('map-canvas'), { 
     center: new google.maps.LatLng(48.1293954, 12.556663), //Setting Initial Position 
     zoom: 10 
    }); 

    var marker = new google.maps.Marker({ 
     map: map, 

     position: new google.maps.LatLng(48.1293954, 12.556663), 
     center: new google.maps.LatLng(48.1293954, 12.556663), 
     zoom: 10 
    }); 

    function newLocation(newLat, newLng) { 
     map.setCenter({ 
      lat: newLat, 
      lng: newLng 
     }); 

     new google.maps.Marker({ 
      map: map, 
      position:{ 
       lat: newLat, 
       lng: newLng 
      } 
     }); 
    } 


    $(".button1").on('click', function() { 
     newLocation(48.1293954, 12.556663); 
    }); 
    $(".button2").on('click', function() { 
     newLocation(51.7163950, -1.2196100); 
    }); 
    $(".button3").on('click', function() { 
     newLocation(55.8610240, -4.2614560); 
    }); 

} 

API調用:

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC7_p0NkDTQQmn8ZVJosJJ-3Xr93x-tVVo&callback=initialize" async defer></script> 
1

您必須在標記實例上的按鈕單擊以及上調用地圖實例上的setCenter方法。您的示例因功能範圍而不起作用。您的的作用域爲initialize函數。您必須在之前刪除var以提升至global/window對象或在您的initialize函數之外創建變量聲明。

var map; 
var marker; 

function initialize() { 
    map = new google.maps.Map(…); 
    marker = new google.maps.Marker(…); 
} 

$('.button').on('click', function() { 
    map.setCenter({ 
    lat: LATITUDE, 
    lng: LONGITUDE 
    }); 

marker.setPosition({ 
    lat: LATITUDE, 
    lng: LONGITUDE 
}); 
});