最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far