最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps setCenter(), v3, when click on element, with jQuery - Stack Overflow

programmeradmin3浏览0评论

When I using this code

<li latlon="58.55801847109299,11.244826413745159" class="point_on_map">Boviken, Hamburgö</li>

And this jQuery code:

$('.point_on_map').click(function(){
    var b = new google.maps.LatLng($(this).attr('latlon'));
    map.setCenter(b);
});

And when I click the center is in the blue, and the marker is in the left upper corner, and not draggable... What have is wrong?

When I using this code

<li latlon="58.55801847109299,11.244826413745159" class="point_on_map">Boviken, Hamburgö</li>

And this jQuery code:

$('.point_on_map').click(function(){
    var b = new google.maps.LatLng($(this).attr('latlon'));
    map.setCenter(b);
});

And when I click the center is in the blue, and the marker is in the left upper corner, and not draggable... What have is wrong?

Share Improve this question asked May 28, 2011 at 9:54 Max AllanMax Allan 6405 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

What you're actually doing there is passing one string to LatLng when you need to be passing it two. That ma in the latlon attribute doesn't get interpretted by JavaScript how you're thinking it does, it just gets seen as part of the string.

You need to split your 58.55801847109299,11.244826413745159 into two variables (e.g. 58.55801847109299 and 11.244826413745159) and pass them separately to LatLng. e.g.

var latlon = $(this).attr('latlon');
var latlon_array = latlon.split(',');
var lat = latlon_array[0];
var lon = latlon_array[1];
var b = new google.maps.LatLng(lat,lon);

That's untested code but hopefully you get the idea.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论