我一直在使用谷歌开发者教程,学习如何将地图添加到我的网站:
https://developers.google.com/maps/tutorials/fundamentals/adding-a-google-map#the_finished_code
我似乎找不到在地图上添加图钉的方法,我使用了"中心"功能,使地图以图钉的纬度和长度为中心,但图钉本身是不可见的。
有没有办法在那个纬度和经度上制作一个别针?
只需要实例化一个标记对象并将其放置在坐标:
<!DOCTYPE html>
<html>
<head>
<style>
#map {
width: 500px;
height: 400px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
var mapCanvas = document.getElementById('map');
var myLatLng = {lat: 44.5403, lng: -78.5463};
var mapOptions = {
center: new google.maps.LatLng(myLatLng),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions)
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>
Fiddle
尝试以下操作:
用location
代替center
之前:
center: new google.maps.LatLng(myLatLng),
之后:
location: new google.maps.LatLng(myLatLng),
这样就可以了(应该自动添加Pin)。