How to update URL with map coordinates?










0















I have a Leaflet map on my React app and I would like to dynamically update the URL with position information (latitude, longitude and zoom) when the map is moved.



Something like: app.com/lat,lng,z/myroutes



Additionally, lat,lng,z should have default values so the app can start at app.com and redirect.



Leaflet has an event listener called onMoveend which gives me the information I want. Question is how to pass it to my Router so it updates the URL and the app internal links.



I am using react-router v4 for the routes.



I've tried creating a custom history object with a basename property attached to the state then passing it to the Router. It works (in terms of updating the URL - in this case, the basename), but the navigation on the app crashes and the console say "You cannot change Router history".



This is the component which updates the URL (this.props.map are from Redux store):



componentDidUpdate(prevProps) 
if (this.props.map !== prevProps.map)
const lat, lng, z = this.props.map
this.setState( lat, lng, z )



render()
const lat, lng, z = this.state

if (lat && lng && z)

const history = createBrowserHistory(
basename: `@$lat,$lng,$z`
)

return (
<Router history=history>
<Main>
<Switch>
<Route exact path='/farms' component=FarmsList />
<Route exact path='/farms/new' component=NewFarm />
<Redirect from='*' to='/farms' />
</Switch>
</Main>
</Router>
)
else
...











share|improve this question
























  • Have you tried working with shouldComponentUpdate()? Show us how you are attempting to change the location as in Minimal, Complete, and Verifiable example

    – charlietfl
    Nov 15 '18 at 19:19












  • Unclear from the question... I think this is what you want? history.push( pathname: '/', search: new URLSearchParams( lat: '10', lng: '20', z: 'whatever...' ).toString(), );

    – SakoBu
    Nov 15 '18 at 19:36











  • Updated the question body. I am not sure if history.push is the solution, because the change must be in the basename. There is a way to handle the problem without setting a basename? I mean, maybe setting lat,lng,z as /:params? Something like: <Route exact path='/:params/farms' component=FarmsList /> <Route exact path='/:params/farms/new' component=NewFarm /> <Redirect from='*' to='/:params/farms' />

    – Rodrigo Mota
    Nov 15 '18 at 21:07















0















I have a Leaflet map on my React app and I would like to dynamically update the URL with position information (latitude, longitude and zoom) when the map is moved.



Something like: app.com/lat,lng,z/myroutes



Additionally, lat,lng,z should have default values so the app can start at app.com and redirect.



Leaflet has an event listener called onMoveend which gives me the information I want. Question is how to pass it to my Router so it updates the URL and the app internal links.



I am using react-router v4 for the routes.



I've tried creating a custom history object with a basename property attached to the state then passing it to the Router. It works (in terms of updating the URL - in this case, the basename), but the navigation on the app crashes and the console say "You cannot change Router history".



This is the component which updates the URL (this.props.map are from Redux store):



componentDidUpdate(prevProps) 
if (this.props.map !== prevProps.map)
const lat, lng, z = this.props.map
this.setState( lat, lng, z )



render()
const lat, lng, z = this.state

if (lat && lng && z)

const history = createBrowserHistory(
basename: `@$lat,$lng,$z`
)

return (
<Router history=history>
<Main>
<Switch>
<Route exact path='/farms' component=FarmsList />
<Route exact path='/farms/new' component=NewFarm />
<Redirect from='*' to='/farms' />
</Switch>
</Main>
</Router>
)
else
...











share|improve this question
























  • Have you tried working with shouldComponentUpdate()? Show us how you are attempting to change the location as in Minimal, Complete, and Verifiable example

    – charlietfl
    Nov 15 '18 at 19:19












  • Unclear from the question... I think this is what you want? history.push( pathname: '/', search: new URLSearchParams( lat: '10', lng: '20', z: 'whatever...' ).toString(), );

    – SakoBu
    Nov 15 '18 at 19:36











  • Updated the question body. I am not sure if history.push is the solution, because the change must be in the basename. There is a way to handle the problem without setting a basename? I mean, maybe setting lat,lng,z as /:params? Something like: <Route exact path='/:params/farms' component=FarmsList /> <Route exact path='/:params/farms/new' component=NewFarm /> <Redirect from='*' to='/:params/farms' />

    – Rodrigo Mota
    Nov 15 '18 at 21:07













0












0








0








I have a Leaflet map on my React app and I would like to dynamically update the URL with position information (latitude, longitude and zoom) when the map is moved.



Something like: app.com/lat,lng,z/myroutes



Additionally, lat,lng,z should have default values so the app can start at app.com and redirect.



Leaflet has an event listener called onMoveend which gives me the information I want. Question is how to pass it to my Router so it updates the URL and the app internal links.



I am using react-router v4 for the routes.



I've tried creating a custom history object with a basename property attached to the state then passing it to the Router. It works (in terms of updating the URL - in this case, the basename), but the navigation on the app crashes and the console say "You cannot change Router history".



This is the component which updates the URL (this.props.map are from Redux store):



componentDidUpdate(prevProps) 
if (this.props.map !== prevProps.map)
const lat, lng, z = this.props.map
this.setState( lat, lng, z )



render()
const lat, lng, z = this.state

if (lat && lng && z)

const history = createBrowserHistory(
basename: `@$lat,$lng,$z`
)

return (
<Router history=history>
<Main>
<Switch>
<Route exact path='/farms' component=FarmsList />
<Route exact path='/farms/new' component=NewFarm />
<Redirect from='*' to='/farms' />
</Switch>
</Main>
</Router>
)
else
...











share|improve this question
















I have a Leaflet map on my React app and I would like to dynamically update the URL with position information (latitude, longitude and zoom) when the map is moved.



Something like: app.com/lat,lng,z/myroutes



Additionally, lat,lng,z should have default values so the app can start at app.com and redirect.



Leaflet has an event listener called onMoveend which gives me the information I want. Question is how to pass it to my Router so it updates the URL and the app internal links.



I am using react-router v4 for the routes.



I've tried creating a custom history object with a basename property attached to the state then passing it to the Router. It works (in terms of updating the URL - in this case, the basename), but the navigation on the app crashes and the console say "You cannot change Router history".



This is the component which updates the URL (this.props.map are from Redux store):



componentDidUpdate(prevProps) 
if (this.props.map !== prevProps.map)
const lat, lng, z = this.props.map
this.setState( lat, lng, z )



render()
const lat, lng, z = this.state

if (lat && lng && z)

const history = createBrowserHistory(
basename: `@$lat,$lng,$z`
)

return (
<Router history=history>
<Main>
<Switch>
<Route exact path='/farms' component=FarmsList />
<Route exact path='/farms/new' component=NewFarm />
<Redirect from='*' to='/farms' />
</Switch>
</Main>
</Router>
)
else
...








javascript reactjs react-router






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 21:02







Rodrigo Mota

















asked Nov 15 '18 at 19:07









Rodrigo MotaRodrigo Mota

1315




1315












  • Have you tried working with shouldComponentUpdate()? Show us how you are attempting to change the location as in Minimal, Complete, and Verifiable example

    – charlietfl
    Nov 15 '18 at 19:19












  • Unclear from the question... I think this is what you want? history.push( pathname: '/', search: new URLSearchParams( lat: '10', lng: '20', z: 'whatever...' ).toString(), );

    – SakoBu
    Nov 15 '18 at 19:36











  • Updated the question body. I am not sure if history.push is the solution, because the change must be in the basename. There is a way to handle the problem without setting a basename? I mean, maybe setting lat,lng,z as /:params? Something like: <Route exact path='/:params/farms' component=FarmsList /> <Route exact path='/:params/farms/new' component=NewFarm /> <Redirect from='*' to='/:params/farms' />

    – Rodrigo Mota
    Nov 15 '18 at 21:07

















  • Have you tried working with shouldComponentUpdate()? Show us how you are attempting to change the location as in Minimal, Complete, and Verifiable example

    – charlietfl
    Nov 15 '18 at 19:19












  • Unclear from the question... I think this is what you want? history.push( pathname: '/', search: new URLSearchParams( lat: '10', lng: '20', z: 'whatever...' ).toString(), );

    – SakoBu
    Nov 15 '18 at 19:36











  • Updated the question body. I am not sure if history.push is the solution, because the change must be in the basename. There is a way to handle the problem without setting a basename? I mean, maybe setting lat,lng,z as /:params? Something like: <Route exact path='/:params/farms' component=FarmsList /> <Route exact path='/:params/farms/new' component=NewFarm /> <Redirect from='*' to='/:params/farms' />

    – Rodrigo Mota
    Nov 15 '18 at 21:07
















Have you tried working with shouldComponentUpdate()? Show us how you are attempting to change the location as in Minimal, Complete, and Verifiable example

– charlietfl
Nov 15 '18 at 19:19






Have you tried working with shouldComponentUpdate()? Show us how you are attempting to change the location as in Minimal, Complete, and Verifiable example

– charlietfl
Nov 15 '18 at 19:19














Unclear from the question... I think this is what you want? history.push( pathname: '/', search: new URLSearchParams( lat: '10', lng: '20', z: 'whatever...' ).toString(), );

– SakoBu
Nov 15 '18 at 19:36





Unclear from the question... I think this is what you want? history.push( pathname: '/', search: new URLSearchParams( lat: '10', lng: '20', z: 'whatever...' ).toString(), );

– SakoBu
Nov 15 '18 at 19:36













Updated the question body. I am not sure if history.push is the solution, because the change must be in the basename. There is a way to handle the problem without setting a basename? I mean, maybe setting lat,lng,z as /:params? Something like: <Route exact path='/:params/farms' component=FarmsList /> <Route exact path='/:params/farms/new' component=NewFarm /> <Redirect from='*' to='/:params/farms' />

– Rodrigo Mota
Nov 15 '18 at 21:07





Updated the question body. I am not sure if history.push is the solution, because the change must be in the basename. There is a way to handle the problem without setting a basename? I mean, maybe setting lat,lng,z as /:params? Something like: <Route exact path='/:params/farms' component=FarmsList /> <Route exact path='/:params/farms/new' component=NewFarm /> <Redirect from='*' to='/:params/farms' />

– Rodrigo Mota
Nov 15 '18 at 21:07












2 Answers
2






active

oldest

votes


















1














Just push the new url onto the history stack,



window.history.pushState(/*empty state object*/, "example name", "/`@$lat,$lng,$z`");


to change the url without causing the browser to load the new page



check out the documentation






share|improve this answer






























    0














    I did this exact thing for a website I built. Didn't use react-router but used my own little url parser thing to update the URL and inform redux of the url params on page load.



    https://github.com/jbccollins/metro-monitor/blob/master/client/src/containers/app/index.js#L55



    Here's where it's called using onMoveEnd
    https://github.com/jbccollins/metro-monitor/blob/master/client/src/containers/MetroMap/index.js#L269



    Note that I added a check to make sure you don't save fractional zoom levels that can happen using mobile pinch zoom. That'll really confuse leaflet and was crashing my app.



    Hope that helps






    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%2f53326358%2fhow-to-update-url-with-map-coordinates%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









      1














      Just push the new url onto the history stack,



      window.history.pushState(/*empty state object*/, "example name", "/`@$lat,$lng,$z`");


      to change the url without causing the browser to load the new page



      check out the documentation






      share|improve this answer



























        1














        Just push the new url onto the history stack,



        window.history.pushState(/*empty state object*/, "example name", "/`@$lat,$lng,$z`");


        to change the url without causing the browser to load the new page



        check out the documentation






        share|improve this answer

























          1












          1








          1







          Just push the new url onto the history stack,



          window.history.pushState(/*empty state object*/, "example name", "/`@$lat,$lng,$z`");


          to change the url without causing the browser to load the new page



          check out the documentation






          share|improve this answer













          Just push the new url onto the history stack,



          window.history.pushState(/*empty state object*/, "example name", "/`@$lat,$lng,$z`");


          to change the url without causing the browser to load the new page



          check out the documentation







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 21:29









          mcmohornmcmohorn

          314




          314























              0














              I did this exact thing for a website I built. Didn't use react-router but used my own little url parser thing to update the URL and inform redux of the url params on page load.



              https://github.com/jbccollins/metro-monitor/blob/master/client/src/containers/app/index.js#L55



              Here's where it's called using onMoveEnd
              https://github.com/jbccollins/metro-monitor/blob/master/client/src/containers/MetroMap/index.js#L269



              Note that I added a check to make sure you don't save fractional zoom levels that can happen using mobile pinch zoom. That'll really confuse leaflet and was crashing my app.



              Hope that helps






              share|improve this answer



























                0














                I did this exact thing for a website I built. Didn't use react-router but used my own little url parser thing to update the URL and inform redux of the url params on page load.



                https://github.com/jbccollins/metro-monitor/blob/master/client/src/containers/app/index.js#L55



                Here's where it's called using onMoveEnd
                https://github.com/jbccollins/metro-monitor/blob/master/client/src/containers/MetroMap/index.js#L269



                Note that I added a check to make sure you don't save fractional zoom levels that can happen using mobile pinch zoom. That'll really confuse leaflet and was crashing my app.



                Hope that helps






                share|improve this answer

























                  0












                  0








                  0







                  I did this exact thing for a website I built. Didn't use react-router but used my own little url parser thing to update the URL and inform redux of the url params on page load.



                  https://github.com/jbccollins/metro-monitor/blob/master/client/src/containers/app/index.js#L55



                  Here's where it's called using onMoveEnd
                  https://github.com/jbccollins/metro-monitor/blob/master/client/src/containers/MetroMap/index.js#L269



                  Note that I added a check to make sure you don't save fractional zoom levels that can happen using mobile pinch zoom. That'll really confuse leaflet and was crashing my app.



                  Hope that helps






                  share|improve this answer













                  I did this exact thing for a website I built. Didn't use react-router but used my own little url parser thing to update the URL and inform redux of the url params on page load.



                  https://github.com/jbccollins/metro-monitor/blob/master/client/src/containers/app/index.js#L55



                  Here's where it's called using onMoveEnd
                  https://github.com/jbccollins/metro-monitor/blob/master/client/src/containers/MetroMap/index.js#L269



                  Note that I added a check to make sure you don't save fractional zoom levels that can happen using mobile pinch zoom. That'll really confuse leaflet and was crashing my app.



                  Hope that helps







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 16:45









                  jbccollinsjbccollins

                  202114




                  202114



























                      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%2f53326358%2fhow-to-update-url-with-map-coordinates%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







                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3