Calculate distance between two lat+lng coordinates in React Native?












7















I've got an object containing the lat and lng of my companys facilities. I've also got an object containing the lat and lng of the user's current location. What I'd like to do is iterate through the object with the location data of the sites, and calculate the closest one to the user's location. I'd like to get the kilometers between the two as well as the time it would take to drive the distance.



I've been looking at Google's Geometry API but can not figure out how to use this in React Native, if it's possible.



My question is what I should do to best achieve this? Can I somehow use the Google Geometry API?



Edit: I don't have to do this in the app, preferably I'd like to do so, but if it's not possible I could simply make a fetch to my PHP API if there is a better solution for PHP.










share|improve this question





























    7















    I've got an object containing the lat and lng of my companys facilities. I've also got an object containing the lat and lng of the user's current location. What I'd like to do is iterate through the object with the location data of the sites, and calculate the closest one to the user's location. I'd like to get the kilometers between the two as well as the time it would take to drive the distance.



    I've been looking at Google's Geometry API but can not figure out how to use this in React Native, if it's possible.



    My question is what I should do to best achieve this? Can I somehow use the Google Geometry API?



    Edit: I don't have to do this in the app, preferably I'd like to do so, but if it's not possible I could simply make a fetch to my PHP API if there is a better solution for PHP.










    share|improve this question



























      7












      7








      7


      2






      I've got an object containing the lat and lng of my companys facilities. I've also got an object containing the lat and lng of the user's current location. What I'd like to do is iterate through the object with the location data of the sites, and calculate the closest one to the user's location. I'd like to get the kilometers between the two as well as the time it would take to drive the distance.



      I've been looking at Google's Geometry API but can not figure out how to use this in React Native, if it's possible.



      My question is what I should do to best achieve this? Can I somehow use the Google Geometry API?



      Edit: I don't have to do this in the app, preferably I'd like to do so, but if it's not possible I could simply make a fetch to my PHP API if there is a better solution for PHP.










      share|improve this question
















      I've got an object containing the lat and lng of my companys facilities. I've also got an object containing the lat and lng of the user's current location. What I'd like to do is iterate through the object with the location data of the sites, and calculate the closest one to the user's location. I'd like to get the kilometers between the two as well as the time it would take to drive the distance.



      I've been looking at Google's Geometry API but can not figure out how to use this in React Native, if it's possible.



      My question is what I should do to best achieve this? Can I somehow use the Google Geometry API?



      Edit: I don't have to do this in the app, preferably I'd like to do so, but if it's not possible I could simply make a fetch to my PHP API if there is a better solution for PHP.







      reactjs react-native






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 21 '17 at 11:43







      Johannes Nyman

















      asked Apr 21 '17 at 8:11









      Johannes NymanJohannes Nyman

      81210




      81210
























          2 Answers
          2






          active

          oldest

          votes


















          10














          The npm package geolib https://github.com/manuelbieh/geolib has several methods that can help you do this; getDistance, findNearest, and orderByDistance.






          share|improve this answer
























          • This looks very promising! Thanks for your reply. I will take a look at this first thing in the morning and get back to you!

            – Johannes Nyman
            Apr 24 '17 at 14:38











          • After testing this, it's exactly what I've been looking for. Thank you!

            – Johannes Nyman
            Apr 26 '17 at 15:09











          • I'm glad it worked for you.

            – reggie3
            Apr 26 '17 at 15:12











          • Is there any way to get the driving distance between two points ??

            – Manish
            Jul 18 '18 at 11:26











          • You'd need something that could route based on roads. Google might have an API for their map directions feature.

            – reggie3
            Jul 18 '18 at 21:51



















          -1














          You can find distance from one point to another using d=√((y2-y1)^2 + (x2-x1)^2). Just do that for each one as you iterate through the set and keep track of the minimum. I don't see why the distance formula would be any different for lat and long but I cant say for sure.






          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%2f43537324%2fcalculate-distance-between-two-latlng-coordinates-in-react-native%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









            10














            The npm package geolib https://github.com/manuelbieh/geolib has several methods that can help you do this; getDistance, findNearest, and orderByDistance.






            share|improve this answer
























            • This looks very promising! Thanks for your reply. I will take a look at this first thing in the morning and get back to you!

              – Johannes Nyman
              Apr 24 '17 at 14:38











            • After testing this, it's exactly what I've been looking for. Thank you!

              – Johannes Nyman
              Apr 26 '17 at 15:09











            • I'm glad it worked for you.

              – reggie3
              Apr 26 '17 at 15:12











            • Is there any way to get the driving distance between two points ??

              – Manish
              Jul 18 '18 at 11:26











            • You'd need something that could route based on roads. Google might have an API for their map directions feature.

              – reggie3
              Jul 18 '18 at 21:51
















            10














            The npm package geolib https://github.com/manuelbieh/geolib has several methods that can help you do this; getDistance, findNearest, and orderByDistance.






            share|improve this answer
























            • This looks very promising! Thanks for your reply. I will take a look at this first thing in the morning and get back to you!

              – Johannes Nyman
              Apr 24 '17 at 14:38











            • After testing this, it's exactly what I've been looking for. Thank you!

              – Johannes Nyman
              Apr 26 '17 at 15:09











            • I'm glad it worked for you.

              – reggie3
              Apr 26 '17 at 15:12











            • Is there any way to get the driving distance between two points ??

              – Manish
              Jul 18 '18 at 11:26











            • You'd need something that could route based on roads. Google might have an API for their map directions feature.

              – reggie3
              Jul 18 '18 at 21:51














            10












            10








            10







            The npm package geolib https://github.com/manuelbieh/geolib has several methods that can help you do this; getDistance, findNearest, and orderByDistance.






            share|improve this answer













            The npm package geolib https://github.com/manuelbieh/geolib has several methods that can help you do this; getDistance, findNearest, and orderByDistance.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 24 '17 at 12:26









            reggie3reggie3

            4461516




            4461516













            • This looks very promising! Thanks for your reply. I will take a look at this first thing in the morning and get back to you!

              – Johannes Nyman
              Apr 24 '17 at 14:38











            • After testing this, it's exactly what I've been looking for. Thank you!

              – Johannes Nyman
              Apr 26 '17 at 15:09











            • I'm glad it worked for you.

              – reggie3
              Apr 26 '17 at 15:12











            • Is there any way to get the driving distance between two points ??

              – Manish
              Jul 18 '18 at 11:26











            • You'd need something that could route based on roads. Google might have an API for their map directions feature.

              – reggie3
              Jul 18 '18 at 21:51



















            • This looks very promising! Thanks for your reply. I will take a look at this first thing in the morning and get back to you!

              – Johannes Nyman
              Apr 24 '17 at 14:38











            • After testing this, it's exactly what I've been looking for. Thank you!

              – Johannes Nyman
              Apr 26 '17 at 15:09











            • I'm glad it worked for you.

              – reggie3
              Apr 26 '17 at 15:12











            • Is there any way to get the driving distance between two points ??

              – Manish
              Jul 18 '18 at 11:26











            • You'd need something that could route based on roads. Google might have an API for their map directions feature.

              – reggie3
              Jul 18 '18 at 21:51

















            This looks very promising! Thanks for your reply. I will take a look at this first thing in the morning and get back to you!

            – Johannes Nyman
            Apr 24 '17 at 14:38





            This looks very promising! Thanks for your reply. I will take a look at this first thing in the morning and get back to you!

            – Johannes Nyman
            Apr 24 '17 at 14:38













            After testing this, it's exactly what I've been looking for. Thank you!

            – Johannes Nyman
            Apr 26 '17 at 15:09





            After testing this, it's exactly what I've been looking for. Thank you!

            – Johannes Nyman
            Apr 26 '17 at 15:09













            I'm glad it worked for you.

            – reggie3
            Apr 26 '17 at 15:12





            I'm glad it worked for you.

            – reggie3
            Apr 26 '17 at 15:12













            Is there any way to get the driving distance between two points ??

            – Manish
            Jul 18 '18 at 11:26





            Is there any way to get the driving distance between two points ??

            – Manish
            Jul 18 '18 at 11:26













            You'd need something that could route based on roads. Google might have an API for their map directions feature.

            – reggie3
            Jul 18 '18 at 21:51





            You'd need something that could route based on roads. Google might have an API for their map directions feature.

            – reggie3
            Jul 18 '18 at 21:51













            -1














            You can find distance from one point to another using d=√((y2-y1)^2 + (x2-x1)^2). Just do that for each one as you iterate through the set and keep track of the minimum. I don't see why the distance formula would be any different for lat and long but I cant say for sure.






            share|improve this answer




























              -1














              You can find distance from one point to another using d=√((y2-y1)^2 + (x2-x1)^2). Just do that for each one as you iterate through the set and keep track of the minimum. I don't see why the distance formula would be any different for lat and long but I cant say for sure.






              share|improve this answer


























                -1












                -1








                -1







                You can find distance from one point to another using d=√((y2-y1)^2 + (x2-x1)^2). Just do that for each one as you iterate through the set and keep track of the minimum. I don't see why the distance formula would be any different for lat and long but I cant say for sure.






                share|improve this answer













                You can find distance from one point to another using d=√((y2-y1)^2 + (x2-x1)^2). Just do that for each one as you iterate through the set and keep track of the minimum. I don't see why the distance formula would be any different for lat and long but I cant say for sure.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 24 '17 at 11:59









                JoeJoe

                8452920




                8452920






























                    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%2f43537324%2fcalculate-distance-between-two-latlng-coordinates-in-react-native%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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python