Leaflet How to set center map when click marker
I follow this example http://jsfiddle.net/abenrob/ZkC5M/ , When i click marker I want map pan to marker and set position center screen my code is below but when i click map.setView(new L.LatLng(position), 5);
is not working.
function markerFunction(id){
for (var i in markers){
var markerID = markers[i].options.title;
var position = markers[i].getLatLng();
if (markerID == id){
map.setView(new L.LatLng(position), 5);
markers[i].openPopup();
};
}
}
Thank you,
leaflet
add a comment |
I follow this example http://jsfiddle.net/abenrob/ZkC5M/ , When i click marker I want map pan to marker and set position center screen my code is below but when i click map.setView(new L.LatLng(position), 5);
is not working.
function markerFunction(id){
for (var i in markers){
var markerID = markers[i].options.title;
var position = markers[i].getLatLng();
if (markerID == id){
map.setView(new L.LatLng(position), 5);
markers[i].openPopup();
};
}
}
Thank you,
leaflet
add a comment |
I follow this example http://jsfiddle.net/abenrob/ZkC5M/ , When i click marker I want map pan to marker and set position center screen my code is below but when i click map.setView(new L.LatLng(position), 5);
is not working.
function markerFunction(id){
for (var i in markers){
var markerID = markers[i].options.title;
var position = markers[i].getLatLng();
if (markerID == id){
map.setView(new L.LatLng(position), 5);
markers[i].openPopup();
};
}
}
Thank you,
leaflet
I follow this example http://jsfiddle.net/abenrob/ZkC5M/ , When i click marker I want map pan to marker and set position center screen my code is below but when i click map.setView(new L.LatLng(position), 5);
is not working.
function markerFunction(id){
for (var i in markers){
var markerID = markers[i].options.title;
var position = markers[i].getLatLng();
if (markerID == id){
map.setView(new L.LatLng(position), 5);
markers[i].openPopup();
};
}
}
Thank you,
leaflet
leaflet
asked Feb 2 '16 at 9:53
droidstackdroidstack
1215
1215
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The example that you're following illustrates how to interact with markers by clicking on links outside of the map, and the function that you're editing is only called when you click on one of those links. It will not affect the behavior of clicking on the markers themselves.
If you replace new L.LatLng(position) with position in your edited function, clicking on the links will cause the map to center on the associated marker, but your question is about how to get the same behavior from clicking on the markers themselves.
To do that, you can create a function to be called when a marker is clicked:
function clickZoom(e) {
map.setView(e.target.getLatLng(),5);
}
and then attach a click event listener to each marker by appending .on('click', clickZoom) when you create the layer, for example:
var marker1 = L.marker([51.497, -0.09], {
title: "marker_1"
}).addTo(map).bindPopup("Marker 1").on('click', clickZoom);
Here is an updated fiddle showing all this at work:
http://jsfiddle.net/ZkC5M/221/
Thank you @nathansnider it's working!!!
– droidstack
Feb 3 '16 at 2:19
add a comment |
panTo() should work too:
marker.addEventListener("click", function (e){
map.panTo(this.getLatLng());
});
for your example, it looks like this:
for(i = 0; i < markers.length; i++){
markers[i].addEventListener("click", function(e){
map.panTo(this.getLatLng());
});
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35150044%2fleaflet-how-to-set-center-map-when-click-marker%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The example that you're following illustrates how to interact with markers by clicking on links outside of the map, and the function that you're editing is only called when you click on one of those links. It will not affect the behavior of clicking on the markers themselves.
If you replace new L.LatLng(position) with position in your edited function, clicking on the links will cause the map to center on the associated marker, but your question is about how to get the same behavior from clicking on the markers themselves.
To do that, you can create a function to be called when a marker is clicked:
function clickZoom(e) {
map.setView(e.target.getLatLng(),5);
}
and then attach a click event listener to each marker by appending .on('click', clickZoom) when you create the layer, for example:
var marker1 = L.marker([51.497, -0.09], {
title: "marker_1"
}).addTo(map).bindPopup("Marker 1").on('click', clickZoom);
Here is an updated fiddle showing all this at work:
http://jsfiddle.net/ZkC5M/221/
Thank you @nathansnider it's working!!!
– droidstack
Feb 3 '16 at 2:19
add a comment |
The example that you're following illustrates how to interact with markers by clicking on links outside of the map, and the function that you're editing is only called when you click on one of those links. It will not affect the behavior of clicking on the markers themselves.
If you replace new L.LatLng(position) with position in your edited function, clicking on the links will cause the map to center on the associated marker, but your question is about how to get the same behavior from clicking on the markers themselves.
To do that, you can create a function to be called when a marker is clicked:
function clickZoom(e) {
map.setView(e.target.getLatLng(),5);
}
and then attach a click event listener to each marker by appending .on('click', clickZoom) when you create the layer, for example:
var marker1 = L.marker([51.497, -0.09], {
title: "marker_1"
}).addTo(map).bindPopup("Marker 1").on('click', clickZoom);
Here is an updated fiddle showing all this at work:
http://jsfiddle.net/ZkC5M/221/
Thank you @nathansnider it's working!!!
– droidstack
Feb 3 '16 at 2:19
add a comment |
The example that you're following illustrates how to interact with markers by clicking on links outside of the map, and the function that you're editing is only called when you click on one of those links. It will not affect the behavior of clicking on the markers themselves.
If you replace new L.LatLng(position) with position in your edited function, clicking on the links will cause the map to center on the associated marker, but your question is about how to get the same behavior from clicking on the markers themselves.
To do that, you can create a function to be called when a marker is clicked:
function clickZoom(e) {
map.setView(e.target.getLatLng(),5);
}
and then attach a click event listener to each marker by appending .on('click', clickZoom) when you create the layer, for example:
var marker1 = L.marker([51.497, -0.09], {
title: "marker_1"
}).addTo(map).bindPopup("Marker 1").on('click', clickZoom);
Here is an updated fiddle showing all this at work:
http://jsfiddle.net/ZkC5M/221/
The example that you're following illustrates how to interact with markers by clicking on links outside of the map, and the function that you're editing is only called when you click on one of those links. It will not affect the behavior of clicking on the markers themselves.
If you replace new L.LatLng(position) with position in your edited function, clicking on the links will cause the map to center on the associated marker, but your question is about how to get the same behavior from clicking on the markers themselves.
To do that, you can create a function to be called when a marker is clicked:
function clickZoom(e) {
map.setView(e.target.getLatLng(),5);
}
and then attach a click event listener to each marker by appending .on('click', clickZoom) when you create the layer, for example:
var marker1 = L.marker([51.497, -0.09], {
title: "marker_1"
}).addTo(map).bindPopup("Marker 1").on('click', clickZoom);
Here is an updated fiddle showing all this at work:
http://jsfiddle.net/ZkC5M/221/
answered Feb 3 '16 at 1:06
nathansnidernathansnider
1,8431610
1,8431610
Thank you @nathansnider it's working!!!
– droidstack
Feb 3 '16 at 2:19
add a comment |
Thank you @nathansnider it's working!!!
– droidstack
Feb 3 '16 at 2:19
Thank you @nathansnider it's working!!!
– droidstack
Feb 3 '16 at 2:19
Thank you @nathansnider it's working!!!
– droidstack
Feb 3 '16 at 2:19
add a comment |
panTo() should work too:
marker.addEventListener("click", function (e){
map.panTo(this.getLatLng());
});
for your example, it looks like this:
for(i = 0; i < markers.length; i++){
markers[i].addEventListener("click", function(e){
map.panTo(this.getLatLng());
});
}
add a comment |
panTo() should work too:
marker.addEventListener("click", function (e){
map.panTo(this.getLatLng());
});
for your example, it looks like this:
for(i = 0; i < markers.length; i++){
markers[i].addEventListener("click", function(e){
map.panTo(this.getLatLng());
});
}
add a comment |
panTo() should work too:
marker.addEventListener("click", function (e){
map.panTo(this.getLatLng());
});
for your example, it looks like this:
for(i = 0; i < markers.length; i++){
markers[i].addEventListener("click", function(e){
map.panTo(this.getLatLng());
});
}
panTo() should work too:
marker.addEventListener("click", function (e){
map.panTo(this.getLatLng());
});
for your example, it looks like this:
for(i = 0; i < markers.length; i++){
markers[i].addEventListener("click", function(e){
map.panTo(this.getLatLng());
});
}
edited Jan 20 at 14:39
answered Jul 4 '17 at 9:30
padegpadeg
255412
255412
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35150044%2fleaflet-how-to-set-center-map-when-click-marker%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown