React-leaflet center of an array of positions












0














Helllo! I'm trying to set the center of the map based the following array:



[[-23.568767,-46.649907],[-23.565972,-46.650859],[-23.563703,-46.653542],[-23.616359,-46.664749],[-23.624203,-46.683369],[-23.565972,-46.650859],[-23.572424,-46.65384],[-23.610235,-46.69591],[-23.609215,-46.69753],[-23.608786,-46.697448],[-23.617262,-46.674802],[-23.620757,-46.673658],[-23.625349,-46.692239],[-23.565972,-46.650859],[-23.564909,-46.654558],[-23.642676,-46.672727],[-23.608786,-46.697448],[-23.610652,-46.686046],[-23.573285,-46.689102],[-23.609215,-46.667182],[-23.609215,-46.667182],[-23.60997,-46.667902],[-23.584718,-46.675473],[-23.584718,-46.675473],[-23.607909,-46.692784],[-23.594718,-46.635473],[-23.564552,-46.654713],[-23.573263,-46.695077],[-23.633372,-46.680053],[-23.64717,-46.727572],[-23.576715,-46.68747],[-23.609215,-46.667182],[-23.609215,-46.667182],[-23.52631,-46.616194],[-23.614064,-46.668883],[-23.608786,-46.697448],[-23.587921,-46.6767],[-23.573691,-46.643678],[-23.573691,-46.643678],[-23.627158,-46.675183],[-23.573263,-46.695077],[-23.573263,-46.695077],[-23.572269,-46.689863],[-23.573263,-46.695077],[-23.628932,-46.665837],[-23.61506,-46.659242],[-23.528071,-46.586955],[-23.595269,-46.669645],[-23.596066,-46.686917],[-23.627158,-46.675183]]


I've tried get the bounds with Leaflet.latLngBounds:



const bounds = L.latLngBounds(latLngs);


and got the following errors:




leaflet-src.js:1658 Uncaught TypeError: Cannot read property 'lat' of
null
at Object.project (leaflet-src.js:1658)
at Object.latLngToPoint (leaflet-src.js:1497)
at NewClass.project (leaflet-src.js:3931)
at NewClass._getNewPixelOrigin (leaflet-src.js:4425)
at NewClass._move (leaflet-src.js:4173)
at NewClass._resetView (leaflet-src.js:4135)
at NewClass.setView (leaflet-src.js:3190)
at NewClass.initialize (leaflet-src.js:3132)
at new NewClass (leaflet-src.js:300)
at Map.createLeafletElement (Map.js:99)



Uncaught TypeError: Cannot read property 'off' of undefined
at Map.componentWillUnmount (Map.js:258)
at callComponentWillUnmountWithTimer (react-dom.development.js:15784)
at HTMLUnknownElement.callCallback (react-dom.development.js:149)
at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
at invokeGuardedCallback (react-dom.development.js:256)
at safelyCallComponentWillUnmount (react-dom.development.js:15791)
at commitUnmount (react-dom.development.js:16159)
at commitNestedUnmounts (react-dom.development.js:16190)
at unmountHostComponents (react-dom.development.js:16446)
at commitDeletion (react-dom.development.js:16498)



The above error occurred in the component:
in Map (created by ShopMap)
in ShopMap (created by App)
in div (created by Context.Consumer)
in StyledComponent (created by styled.div)
in div (created by App)
in div (created by Context.Consumer)
in StyledComponent (created by styled.div)
in App (created by AppContainer)
in AppContainer (created by Connect(AppContainer))
in Connect(AppContainer)
in Provider



Uncaught Error: A cross-origin error was thrown. React doesn't have
access to the actual error object in development.
at Object.invokeGuardedCallbackDev (react-dom.development.js:210)
at invokeGuardedCallback (react-dom.development.js:256)
at commitRoot (react-dom.development.js:17442)
at completeRoot (react-dom.development.js:18898)
at performWorkOnRoot (react-dom.development.js:18827)
at performWork (react-dom.development.js:18735)
at performSyncWork (react-dom.development.js:18709)
at requestWork (react-dom.development.js:18578)
at scheduleWork (react-dom.development.js:18387)
at scheduleRootUpdate (react-dom.development.js:19055)




Then I tried create a FeatureGroup and add Markers based on the array:



const group = new L.FeatureGroup();

positions.forEach((latitude, longitude) => {
L.marker([latitude, longitude]).addTo(group);
});


But results on the following:



LatLngBounds {}


And the Map component:



const ShopMap = ({ shops }) => {
const positions = shops.reduce((newArray, shop) => {
return [
...newArray,
{
latitude: shop.latitude,
longitude: shop.longitude
}
]
}, );

return (
<Map center={getLatLngBounds(positions)} zoom={15}>
<TileLayer
attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
{shops &&
shops.map(({ latitude, longitude, revenueLowerThanExpected, name }) => {
return (
<Marker
key={name}
icon={revenueLowerThanExpected ? redMarker : blueMarker}
position={[
latitude,
longitude
]}
/>
);
})}
</Map>
);
};


Someone could help me with this?










share|improve this question



























    0














    Helllo! I'm trying to set the center of the map based the following array:



    [[-23.568767,-46.649907],[-23.565972,-46.650859],[-23.563703,-46.653542],[-23.616359,-46.664749],[-23.624203,-46.683369],[-23.565972,-46.650859],[-23.572424,-46.65384],[-23.610235,-46.69591],[-23.609215,-46.69753],[-23.608786,-46.697448],[-23.617262,-46.674802],[-23.620757,-46.673658],[-23.625349,-46.692239],[-23.565972,-46.650859],[-23.564909,-46.654558],[-23.642676,-46.672727],[-23.608786,-46.697448],[-23.610652,-46.686046],[-23.573285,-46.689102],[-23.609215,-46.667182],[-23.609215,-46.667182],[-23.60997,-46.667902],[-23.584718,-46.675473],[-23.584718,-46.675473],[-23.607909,-46.692784],[-23.594718,-46.635473],[-23.564552,-46.654713],[-23.573263,-46.695077],[-23.633372,-46.680053],[-23.64717,-46.727572],[-23.576715,-46.68747],[-23.609215,-46.667182],[-23.609215,-46.667182],[-23.52631,-46.616194],[-23.614064,-46.668883],[-23.608786,-46.697448],[-23.587921,-46.6767],[-23.573691,-46.643678],[-23.573691,-46.643678],[-23.627158,-46.675183],[-23.573263,-46.695077],[-23.573263,-46.695077],[-23.572269,-46.689863],[-23.573263,-46.695077],[-23.628932,-46.665837],[-23.61506,-46.659242],[-23.528071,-46.586955],[-23.595269,-46.669645],[-23.596066,-46.686917],[-23.627158,-46.675183]]


    I've tried get the bounds with Leaflet.latLngBounds:



    const bounds = L.latLngBounds(latLngs);


    and got the following errors:




    leaflet-src.js:1658 Uncaught TypeError: Cannot read property 'lat' of
    null
    at Object.project (leaflet-src.js:1658)
    at Object.latLngToPoint (leaflet-src.js:1497)
    at NewClass.project (leaflet-src.js:3931)
    at NewClass._getNewPixelOrigin (leaflet-src.js:4425)
    at NewClass._move (leaflet-src.js:4173)
    at NewClass._resetView (leaflet-src.js:4135)
    at NewClass.setView (leaflet-src.js:3190)
    at NewClass.initialize (leaflet-src.js:3132)
    at new NewClass (leaflet-src.js:300)
    at Map.createLeafletElement (Map.js:99)



    Uncaught TypeError: Cannot read property 'off' of undefined
    at Map.componentWillUnmount (Map.js:258)
    at callComponentWillUnmountWithTimer (react-dom.development.js:15784)
    at HTMLUnknownElement.callCallback (react-dom.development.js:149)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
    at invokeGuardedCallback (react-dom.development.js:256)
    at safelyCallComponentWillUnmount (react-dom.development.js:15791)
    at commitUnmount (react-dom.development.js:16159)
    at commitNestedUnmounts (react-dom.development.js:16190)
    at unmountHostComponents (react-dom.development.js:16446)
    at commitDeletion (react-dom.development.js:16498)



    The above error occurred in the component:
    in Map (created by ShopMap)
    in ShopMap (created by App)
    in div (created by Context.Consumer)
    in StyledComponent (created by styled.div)
    in div (created by App)
    in div (created by Context.Consumer)
    in StyledComponent (created by styled.div)
    in App (created by AppContainer)
    in AppContainer (created by Connect(AppContainer))
    in Connect(AppContainer)
    in Provider



    Uncaught Error: A cross-origin error was thrown. React doesn't have
    access to the actual error object in development.
    at Object.invokeGuardedCallbackDev (react-dom.development.js:210)
    at invokeGuardedCallback (react-dom.development.js:256)
    at commitRoot (react-dom.development.js:17442)
    at completeRoot (react-dom.development.js:18898)
    at performWorkOnRoot (react-dom.development.js:18827)
    at performWork (react-dom.development.js:18735)
    at performSyncWork (react-dom.development.js:18709)
    at requestWork (react-dom.development.js:18578)
    at scheduleWork (react-dom.development.js:18387)
    at scheduleRootUpdate (react-dom.development.js:19055)




    Then I tried create a FeatureGroup and add Markers based on the array:



    const group = new L.FeatureGroup();

    positions.forEach((latitude, longitude) => {
    L.marker([latitude, longitude]).addTo(group);
    });


    But results on the following:



    LatLngBounds {}


    And the Map component:



    const ShopMap = ({ shops }) => {
    const positions = shops.reduce((newArray, shop) => {
    return [
    ...newArray,
    {
    latitude: shop.latitude,
    longitude: shop.longitude
    }
    ]
    }, );

    return (
    <Map center={getLatLngBounds(positions)} zoom={15}>
    <TileLayer
    attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    />
    {shops &&
    shops.map(({ latitude, longitude, revenueLowerThanExpected, name }) => {
    return (
    <Marker
    key={name}
    icon={revenueLowerThanExpected ? redMarker : blueMarker}
    position={[
    latitude,
    longitude
    ]}
    />
    );
    })}
    </Map>
    );
    };


    Someone could help me with this?










    share|improve this question

























      0












      0








      0


      0





      Helllo! I'm trying to set the center of the map based the following array:



      [[-23.568767,-46.649907],[-23.565972,-46.650859],[-23.563703,-46.653542],[-23.616359,-46.664749],[-23.624203,-46.683369],[-23.565972,-46.650859],[-23.572424,-46.65384],[-23.610235,-46.69591],[-23.609215,-46.69753],[-23.608786,-46.697448],[-23.617262,-46.674802],[-23.620757,-46.673658],[-23.625349,-46.692239],[-23.565972,-46.650859],[-23.564909,-46.654558],[-23.642676,-46.672727],[-23.608786,-46.697448],[-23.610652,-46.686046],[-23.573285,-46.689102],[-23.609215,-46.667182],[-23.609215,-46.667182],[-23.60997,-46.667902],[-23.584718,-46.675473],[-23.584718,-46.675473],[-23.607909,-46.692784],[-23.594718,-46.635473],[-23.564552,-46.654713],[-23.573263,-46.695077],[-23.633372,-46.680053],[-23.64717,-46.727572],[-23.576715,-46.68747],[-23.609215,-46.667182],[-23.609215,-46.667182],[-23.52631,-46.616194],[-23.614064,-46.668883],[-23.608786,-46.697448],[-23.587921,-46.6767],[-23.573691,-46.643678],[-23.573691,-46.643678],[-23.627158,-46.675183],[-23.573263,-46.695077],[-23.573263,-46.695077],[-23.572269,-46.689863],[-23.573263,-46.695077],[-23.628932,-46.665837],[-23.61506,-46.659242],[-23.528071,-46.586955],[-23.595269,-46.669645],[-23.596066,-46.686917],[-23.627158,-46.675183]]


      I've tried get the bounds with Leaflet.latLngBounds:



      const bounds = L.latLngBounds(latLngs);


      and got the following errors:




      leaflet-src.js:1658 Uncaught TypeError: Cannot read property 'lat' of
      null
      at Object.project (leaflet-src.js:1658)
      at Object.latLngToPoint (leaflet-src.js:1497)
      at NewClass.project (leaflet-src.js:3931)
      at NewClass._getNewPixelOrigin (leaflet-src.js:4425)
      at NewClass._move (leaflet-src.js:4173)
      at NewClass._resetView (leaflet-src.js:4135)
      at NewClass.setView (leaflet-src.js:3190)
      at NewClass.initialize (leaflet-src.js:3132)
      at new NewClass (leaflet-src.js:300)
      at Map.createLeafletElement (Map.js:99)



      Uncaught TypeError: Cannot read property 'off' of undefined
      at Map.componentWillUnmount (Map.js:258)
      at callComponentWillUnmountWithTimer (react-dom.development.js:15784)
      at HTMLUnknownElement.callCallback (react-dom.development.js:149)
      at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
      at invokeGuardedCallback (react-dom.development.js:256)
      at safelyCallComponentWillUnmount (react-dom.development.js:15791)
      at commitUnmount (react-dom.development.js:16159)
      at commitNestedUnmounts (react-dom.development.js:16190)
      at unmountHostComponents (react-dom.development.js:16446)
      at commitDeletion (react-dom.development.js:16498)



      The above error occurred in the component:
      in Map (created by ShopMap)
      in ShopMap (created by App)
      in div (created by Context.Consumer)
      in StyledComponent (created by styled.div)
      in div (created by App)
      in div (created by Context.Consumer)
      in StyledComponent (created by styled.div)
      in App (created by AppContainer)
      in AppContainer (created by Connect(AppContainer))
      in Connect(AppContainer)
      in Provider



      Uncaught Error: A cross-origin error was thrown. React doesn't have
      access to the actual error object in development.
      at Object.invokeGuardedCallbackDev (react-dom.development.js:210)
      at invokeGuardedCallback (react-dom.development.js:256)
      at commitRoot (react-dom.development.js:17442)
      at completeRoot (react-dom.development.js:18898)
      at performWorkOnRoot (react-dom.development.js:18827)
      at performWork (react-dom.development.js:18735)
      at performSyncWork (react-dom.development.js:18709)
      at requestWork (react-dom.development.js:18578)
      at scheduleWork (react-dom.development.js:18387)
      at scheduleRootUpdate (react-dom.development.js:19055)




      Then I tried create a FeatureGroup and add Markers based on the array:



      const group = new L.FeatureGroup();

      positions.forEach((latitude, longitude) => {
      L.marker([latitude, longitude]).addTo(group);
      });


      But results on the following:



      LatLngBounds {}


      And the Map component:



      const ShopMap = ({ shops }) => {
      const positions = shops.reduce((newArray, shop) => {
      return [
      ...newArray,
      {
      latitude: shop.latitude,
      longitude: shop.longitude
      }
      ]
      }, );

      return (
      <Map center={getLatLngBounds(positions)} zoom={15}>
      <TileLayer
      attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
      />
      {shops &&
      shops.map(({ latitude, longitude, revenueLowerThanExpected, name }) => {
      return (
      <Marker
      key={name}
      icon={revenueLowerThanExpected ? redMarker : blueMarker}
      position={[
      latitude,
      longitude
      ]}
      />
      );
      })}
      </Map>
      );
      };


      Someone could help me with this?










      share|improve this question













      Helllo! I'm trying to set the center of the map based the following array:



      [[-23.568767,-46.649907],[-23.565972,-46.650859],[-23.563703,-46.653542],[-23.616359,-46.664749],[-23.624203,-46.683369],[-23.565972,-46.650859],[-23.572424,-46.65384],[-23.610235,-46.69591],[-23.609215,-46.69753],[-23.608786,-46.697448],[-23.617262,-46.674802],[-23.620757,-46.673658],[-23.625349,-46.692239],[-23.565972,-46.650859],[-23.564909,-46.654558],[-23.642676,-46.672727],[-23.608786,-46.697448],[-23.610652,-46.686046],[-23.573285,-46.689102],[-23.609215,-46.667182],[-23.609215,-46.667182],[-23.60997,-46.667902],[-23.584718,-46.675473],[-23.584718,-46.675473],[-23.607909,-46.692784],[-23.594718,-46.635473],[-23.564552,-46.654713],[-23.573263,-46.695077],[-23.633372,-46.680053],[-23.64717,-46.727572],[-23.576715,-46.68747],[-23.609215,-46.667182],[-23.609215,-46.667182],[-23.52631,-46.616194],[-23.614064,-46.668883],[-23.608786,-46.697448],[-23.587921,-46.6767],[-23.573691,-46.643678],[-23.573691,-46.643678],[-23.627158,-46.675183],[-23.573263,-46.695077],[-23.573263,-46.695077],[-23.572269,-46.689863],[-23.573263,-46.695077],[-23.628932,-46.665837],[-23.61506,-46.659242],[-23.528071,-46.586955],[-23.595269,-46.669645],[-23.596066,-46.686917],[-23.627158,-46.675183]]


      I've tried get the bounds with Leaflet.latLngBounds:



      const bounds = L.latLngBounds(latLngs);


      and got the following errors:




      leaflet-src.js:1658 Uncaught TypeError: Cannot read property 'lat' of
      null
      at Object.project (leaflet-src.js:1658)
      at Object.latLngToPoint (leaflet-src.js:1497)
      at NewClass.project (leaflet-src.js:3931)
      at NewClass._getNewPixelOrigin (leaflet-src.js:4425)
      at NewClass._move (leaflet-src.js:4173)
      at NewClass._resetView (leaflet-src.js:4135)
      at NewClass.setView (leaflet-src.js:3190)
      at NewClass.initialize (leaflet-src.js:3132)
      at new NewClass (leaflet-src.js:300)
      at Map.createLeafletElement (Map.js:99)



      Uncaught TypeError: Cannot read property 'off' of undefined
      at Map.componentWillUnmount (Map.js:258)
      at callComponentWillUnmountWithTimer (react-dom.development.js:15784)
      at HTMLUnknownElement.callCallback (react-dom.development.js:149)
      at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
      at invokeGuardedCallback (react-dom.development.js:256)
      at safelyCallComponentWillUnmount (react-dom.development.js:15791)
      at commitUnmount (react-dom.development.js:16159)
      at commitNestedUnmounts (react-dom.development.js:16190)
      at unmountHostComponents (react-dom.development.js:16446)
      at commitDeletion (react-dom.development.js:16498)



      The above error occurred in the component:
      in Map (created by ShopMap)
      in ShopMap (created by App)
      in div (created by Context.Consumer)
      in StyledComponent (created by styled.div)
      in div (created by App)
      in div (created by Context.Consumer)
      in StyledComponent (created by styled.div)
      in App (created by AppContainer)
      in AppContainer (created by Connect(AppContainer))
      in Connect(AppContainer)
      in Provider



      Uncaught Error: A cross-origin error was thrown. React doesn't have
      access to the actual error object in development.
      at Object.invokeGuardedCallbackDev (react-dom.development.js:210)
      at invokeGuardedCallback (react-dom.development.js:256)
      at commitRoot (react-dom.development.js:17442)
      at completeRoot (react-dom.development.js:18898)
      at performWorkOnRoot (react-dom.development.js:18827)
      at performWork (react-dom.development.js:18735)
      at performSyncWork (react-dom.development.js:18709)
      at requestWork (react-dom.development.js:18578)
      at scheduleWork (react-dom.development.js:18387)
      at scheduleRootUpdate (react-dom.development.js:19055)




      Then I tried create a FeatureGroup and add Markers based on the array:



      const group = new L.FeatureGroup();

      positions.forEach((latitude, longitude) => {
      L.marker([latitude, longitude]).addTo(group);
      });


      But results on the following:



      LatLngBounds {}


      And the Map component:



      const ShopMap = ({ shops }) => {
      const positions = shops.reduce((newArray, shop) => {
      return [
      ...newArray,
      {
      latitude: shop.latitude,
      longitude: shop.longitude
      }
      ]
      }, );

      return (
      <Map center={getLatLngBounds(positions)} zoom={15}>
      <TileLayer
      attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
      />
      {shops &&
      shops.map(({ latitude, longitude, revenueLowerThanExpected, name }) => {
      return (
      <Marker
      key={name}
      icon={revenueLowerThanExpected ? redMarker : blueMarker}
      position={[
      latitude,
      longitude
      ]}
      />
      );
      })}
      </Map>
      );
      };


      Someone could help me with this?







      reactjs react-leaflet






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 16:34









      Breno Augusto

      14




      14
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Map.center property expects a latLng value but what you are after is Map.bounds property which:




          A rectangle for the map to contain. It will be centered, and the map
          will zoom in as close as it can while still showing the full bounds




          In case of Map.bounds value, it could be provided as an array of {lat,lng} values, in your case



          const getLatLngBounds = () => {
          return [
          [-23.568767, -46.649907],
          [-23.565972, -46.650859],
          [-23.563703, -46.653542],
          [-23.616359, -46.664749],
          [-23.624203, -46.683369],
          ...
          ];
          }




          <Map bounds={getLatLngBounds()} >
          <TileLayer
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'/>
          </Map>


          Refer this example for a more details.



          Or as L.latLngBounds value, here is a demo for your reference.






          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%2f53266418%2freact-leaflet-center-of-an-array-of-positions%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Map.center property expects a latLng value but what you are after is Map.bounds property which:




            A rectangle for the map to contain. It will be centered, and the map
            will zoom in as close as it can while still showing the full bounds




            In case of Map.bounds value, it could be provided as an array of {lat,lng} values, in your case



            const getLatLngBounds = () => {
            return [
            [-23.568767, -46.649907],
            [-23.565972, -46.650859],
            [-23.563703, -46.653542],
            [-23.616359, -46.664749],
            [-23.624203, -46.683369],
            ...
            ];
            }




            <Map bounds={getLatLngBounds()} >
            <TileLayer
            url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
            attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'/>
            </Map>


            Refer this example for a more details.



            Or as L.latLngBounds value, here is a demo for your reference.






            share|improve this answer




























              0














              Map.center property expects a latLng value but what you are after is Map.bounds property which:




              A rectangle for the map to contain. It will be centered, and the map
              will zoom in as close as it can while still showing the full bounds




              In case of Map.bounds value, it could be provided as an array of {lat,lng} values, in your case



              const getLatLngBounds = () => {
              return [
              [-23.568767, -46.649907],
              [-23.565972, -46.650859],
              [-23.563703, -46.653542],
              [-23.616359, -46.664749],
              [-23.624203, -46.683369],
              ...
              ];
              }




              <Map bounds={getLatLngBounds()} >
              <TileLayer
              url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
              attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'/>
              </Map>


              Refer this example for a more details.



              Or as L.latLngBounds value, here is a demo for your reference.






              share|improve this answer


























                0












                0








                0






                Map.center property expects a latLng value but what you are after is Map.bounds property which:




                A rectangle for the map to contain. It will be centered, and the map
                will zoom in as close as it can while still showing the full bounds




                In case of Map.bounds value, it could be provided as an array of {lat,lng} values, in your case



                const getLatLngBounds = () => {
                return [
                [-23.568767, -46.649907],
                [-23.565972, -46.650859],
                [-23.563703, -46.653542],
                [-23.616359, -46.664749],
                [-23.624203, -46.683369],
                ...
                ];
                }




                <Map bounds={getLatLngBounds()} >
                <TileLayer
                url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'/>
                </Map>


                Refer this example for a more details.



                Or as L.latLngBounds value, here is a demo for your reference.






                share|improve this answer














                Map.center property expects a latLng value but what you are after is Map.bounds property which:




                A rectangle for the map to contain. It will be centered, and the map
                will zoom in as close as it can while still showing the full bounds




                In case of Map.bounds value, it could be provided as an array of {lat,lng} values, in your case



                const getLatLngBounds = () => {
                return [
                [-23.568767, -46.649907],
                [-23.565972, -46.650859],
                [-23.563703, -46.653542],
                [-23.616359, -46.664749],
                [-23.624203, -46.683369],
                ...
                ];
                }




                <Map bounds={getLatLngBounds()} >
                <TileLayer
                url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'/>
                </Map>


                Refer this example for a more details.



                Or as L.latLngBounds value, here is a demo for your reference.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 24 at 12:36

























                answered Nov 14 at 13:29









                Vadim Gremyachev

                34.2k769110




                34.2k769110






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53266418%2freact-leaflet-center-of-an-array-of-positions%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

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly