Leaflet How to set center map when click marker












1















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,










share|improve this question



























    1















    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,










    share|improve this question

























      1












      1








      1


      2






      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,










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 2 '16 at 9:53









      droidstackdroidstack

      1215




      1215
























          2 Answers
          2






          active

          oldest

          votes


















          6














          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/






          share|improve this answer
























          • Thank you @nathansnider it's working!!!

            – droidstack
            Feb 3 '16 at 2:19



















          5














          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());
          });
          }





          share|improve this answer

























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            6














            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/






            share|improve this answer
























            • Thank you @nathansnider it's working!!!

              – droidstack
              Feb 3 '16 at 2:19
















            6














            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/






            share|improve this answer
























            • Thank you @nathansnider it's working!!!

              – droidstack
              Feb 3 '16 at 2:19














            6












            6








            6







            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/






            share|improve this answer













            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/







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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



















            • 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













            5














            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());
            });
            }





            share|improve this answer






























              5














              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());
              });
              }





              share|improve this answer




























                5












                5








                5







                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());
                });
                }





                share|improve this answer















                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());
                });
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 20 at 14:39

























                answered Jul 4 '17 at 9:30









                padegpadeg

                255412




                255412






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Callistus III

                    Ostreoida

                    Plistias Cous