我有一个简单的位置列表,其中lat和lon存储为数据attr
<a name="locations"></a>
<ul>
<li><a class="location" data-location="52.240477,-0.902656">northampton</a></li>
<li><a class="location" data-location="51.454265,-0.97813">reading</a></li>
<li><a class="location" data-location="51.262251,-0.467252">surrey</a></li>
<li><a class="location" data-location="51.555774,-1.779718">swindon</a></li>
<li><a class="location" data-location="51.864211,-2.238033">gloucestershire</a></li>
</ul>
和onClick我想将地图平移到点击的位置
function toggleBounce() {
if (marker.getAnimation() != null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
}
$('.location').on('click',function(){
pan($(this).data('location'));
});
function pan(latlon) {
var panPoint = new google.maps.LatLng(latlon);
map.panTo(panPoint)
}
var map;
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng({{$dailydeal[0]['locations'][0]['lat']}}, {{$dailydeal[0]['locations'][0]['lon']}}),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
marker = new google.maps.Marker({
map:map,
draggable:true,
animation: google.maps.Animation.DROP,
position: new google.maps.LatLng({{$dailydeal[0]['locations'][0]['lat']}}, {{$dailydeal[0]['locations'][0]['lon']}})
});
google.maps.event.addListener(marker, 'click', toggleBounce);
}
google.maps.event.addDomListener(window, 'load', initialize);
但是我得到了太多的递归错误
答案 0 :(得分:1)
panTo的参数必须是google.maps.LatLng个对象或LatLngLiteral
你给它的是什么(它是一个逗号分隔的字符串恰好包含纬度和经度:
<li><a class="location" data-location="52.240477,-0.902656">northampton</a></li>
$('.location').on('click',function(){
pan($(this).data('location'));
});
function pan(latlon) {
var panPoint = new google.maps.LatLng(latlon);
map.panTo(panPoint)
}
工作代码段:
var map;
function pan(latlon) {
var coords = latlon.split(",");
var panPoint = new google.maps.LatLng(coords[0], coords[1]);
map.panTo(panPoint)
}
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(0, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
marker = new google.maps.Marker({
map: map,
draggable: true,
animation: google.maps.Animation.DROP,
position: new google.maps.LatLng(0, 0)
});
$('.location').on('click', function() {
pan($(this).data('location'));
});
google.maps.event.addListener(marker, 'click', toggleBounce);
}
function toggleBounce() {
if (marker.getAnimation() != null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<a name="locations"></a>
<ul>
<li><a class="location" data-location="52.240477,-0.902656">northampton</a>
</li>
<li><a class="location" data-location="51.454265,-0.97813">reading</a>
</li>
<li><a class="location" data-location="51.262251,-0.467252">surrey</a>
</li>
<li><a class="location" data-location="51.555774,-1.779718">swindon</a>
</li>
<li><a class="location" data-location="51.864211,-2.238033">gloucestershire</a>
</li>
</ul>
<div id="map-canvas"></div>