如何在具有两个位置的 google 地图上添加标记?
How to add marker on google map with two locations?
我想在 google 地图上添加两个可点击的标记。当我点击按钮时,它应该改变地图标记的位置。
<script>
var map;
function initialize()
{
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(52.302516, 16.414546), //Setting Initial Position
zoom: 14,
});
var marker = new google.maps.Marker({
position: newLocation(),
map: map,
title: 'AGM-CORP',
icon: 'img/agm-marker.png'
});
}
function newLocation(newLat, newLng)
{
map.setCenter({
lat: newLat,
lng: newLng
});
}
google.maps.event.addDomListener(window, 'load', initialize);
$(document).ready(function ()
{
$("#1").on('click', function ()
{
newLocation(52.302516, 16.414546);
});
$("#2").on('click', function ()
{
newLocation(51.706478, 15.274753);
});
});
</script>
<div id="map-canvas"></div>
</div>
<h3>Zobacz lokalizację:</h3>
<button id="1" style="padding:10px; cursor:pointer;">Siedziba Firmy</button>
<button id="2" style="padding:10px;cursor:pointer;">Kopalnia Kruszyw</button>
</div>
此函数只会切换视图的位置:
map.setCenter({
lat: newLat,
lng: newLng
});
改用这个:
new google.maps.LatLng(-25.363882,131.044922);
其次,您需要将事件监听器添加到标记对象以使其正常工作:
// create markers on the map
marker1 = new google.maps.Marker({ ... })
marker2 = new google.maps.Marker({ ... })
marker3 = new google.maps.Marker({ ... })
// add event listener
marker1.addListener('click', function() { ... })
marker2.addListener('click', function() { ... })
进一步检查 docs 它们非常简单。
var map = null
var marker = null;
var myLatLng = {
lat: 52.302516,
lng: 16.414546
};
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(myLatLng.lat, myLatLng.lng),
zoom: 14,
});
marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
}
function updateMap(lat, lng) {;
myLatLng.lat = lat;
myLatLng.lng = lng
map.setCenter(myLatLng);
marker.setPosition(myLatLng);
}
$(document).ready(function() {
$("#1").on('click', function() {
updateMap(52.302516, 16.414546);
});
$("#2").on('click', function() {
updateMap(51.706478, 15.274753);
});
});
我正在使用新标记初始化地图。
Working jffiddle is here
这会奏效。有一个保存 marker
的全局变量。每当位置改变时,使用setPosition
方法将标记设置在Lat
和Lng
的位置,并使用setCenter
方法在中心显示标记。无需每次都初始化地图。
var map,marker;
function initialize()
{
map = new google.maps.Map(document.getElementById('googleMap'), {
center: new google.maps.LatLng(52.302516, 16.414546), //Setting Initial Position
zoom: 6,
});
setLocation(52.302516,16.414546);
}
function setLocation(newLat, newLng)
{
var latlng = new google.maps.LatLng(newLat,newLng);
if(marker) //Remove the marker, if already set
{
marker.setMap(null);
}
marker = new google.maps.Marker({
position: latlng,
map: map,
title: 'AGM-CORP'
});
map.setCenter(latlng);
}
$(document).ready(function ()
{
$("#1").on('click', function ()
{
setLocation(13.070814558816117, 80.2656996835234);
});
$("#2").on('click', function ()
{
setLocation(59.4739316352335,-110.89646088128342);
});
});
我想在 google 地图上添加两个可点击的标记。当我点击按钮时,它应该改变地图标记的位置。
<script>
var map;
function initialize()
{
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(52.302516, 16.414546), //Setting Initial Position
zoom: 14,
});
var marker = new google.maps.Marker({
position: newLocation(),
map: map,
title: 'AGM-CORP',
icon: 'img/agm-marker.png'
});
}
function newLocation(newLat, newLng)
{
map.setCenter({
lat: newLat,
lng: newLng
});
}
google.maps.event.addDomListener(window, 'load', initialize);
$(document).ready(function ()
{
$("#1").on('click', function ()
{
newLocation(52.302516, 16.414546);
});
$("#2").on('click', function ()
{
newLocation(51.706478, 15.274753);
});
});
</script>
<div id="map-canvas"></div>
</div>
<h3>Zobacz lokalizację:</h3>
<button id="1" style="padding:10px; cursor:pointer;">Siedziba Firmy</button>
<button id="2" style="padding:10px;cursor:pointer;">Kopalnia Kruszyw</button>
</div>
此函数只会切换视图的位置:
map.setCenter({
lat: newLat,
lng: newLng
});
改用这个:
new google.maps.LatLng(-25.363882,131.044922);
其次,您需要将事件监听器添加到标记对象以使其正常工作:
// create markers on the map
marker1 = new google.maps.Marker({ ... })
marker2 = new google.maps.Marker({ ... })
marker3 = new google.maps.Marker({ ... })
// add event listener
marker1.addListener('click', function() { ... })
marker2.addListener('click', function() { ... })
进一步检查 docs 它们非常简单。
var map = null
var marker = null;
var myLatLng = {
lat: 52.302516,
lng: 16.414546
};
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(myLatLng.lat, myLatLng.lng),
zoom: 14,
});
marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
}
function updateMap(lat, lng) {;
myLatLng.lat = lat;
myLatLng.lng = lng
map.setCenter(myLatLng);
marker.setPosition(myLatLng);
}
$(document).ready(function() {
$("#1").on('click', function() {
updateMap(52.302516, 16.414546);
});
$("#2").on('click', function() {
updateMap(51.706478, 15.274753);
});
});
我正在使用新标记初始化地图。 Working jffiddle is here
这会奏效。有一个保存 marker
的全局变量。每当位置改变时,使用setPosition
方法将标记设置在Lat
和Lng
的位置,并使用setCenter
方法在中心显示标记。无需每次都初始化地图。
var map,marker;
function initialize()
{
map = new google.maps.Map(document.getElementById('googleMap'), {
center: new google.maps.LatLng(52.302516, 16.414546), //Setting Initial Position
zoom: 6,
});
setLocation(52.302516,16.414546);
}
function setLocation(newLat, newLng)
{
var latlng = new google.maps.LatLng(newLat,newLng);
if(marker) //Remove the marker, if already set
{
marker.setMap(null);
}
marker = new google.maps.Marker({
position: latlng,
map: map,
title: 'AGM-CORP'
});
map.setCenter(latlng);
}
$(document).ready(function ()
{
$("#1").on('click', function ()
{
setLocation(13.070814558816117, 80.2656996835234);
});
$("#2").on('click', function ()
{
setLocation(59.4739316352335,-110.89646088128342);
});
});