locked
Mobile phone gps coordinates RRS feed

  • Question

  • User-1852975005 posted

    Hi,
    is it possible to get gps coordinates from some ones mobile phone when they are connected to your web app.


    Thanks,

    Friday, March 13, 2020 9:51 AM

All replies

  • User475983607 posted

    See geolocation documentation.

    https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API

    Friday, March 13, 2020 12:04 PM
  • User-1852975005 posted

    Hi, looks good.

    Excuse my ignorance, but it is in Java.
    I'm I able to grab the longitude and latitude into C# by some means?

    Thanks,

     

    Friday, March 13, 2020 2:37 PM
  • User475983607 posted

    Excuse my ignorance, but it is in Java.
    I'm I able to grab the longitude and latitude into C# by some means?

    No, the code is JavaScript which runs in the browser.  C# cannot find the user location since C# runs on the server not the client.  I assume you are building a browser based application.  If not, then you are probably in the wrong forum and need maybe mobile application support.

    Friday, March 13, 2020 4:21 PM
  • User-1852975005 posted

    Hi,
    its a shame that there is no way of capturing the values in to C# then.
    As I would have liked to have done a comparison to see if the user using the phone was in the correct location, there abouts.

    Thanks,

    Friday, March 13, 2020 8:07 PM
  • User475983607 posted

    Hi,
    its a shame that there is no way of capturing the values in to C# then.
    As I would have liked to have done a comparison to see if the user using the phone was in the correct location, there abouts.

    Thanks,

    You misunderstand.  If your C# application is built to run on a mobile device then certainly the mobile application can get the phone GPS location.  Web API is a serer application.  The code is running on the server not the mobile device.  Therefore, the mobile device must send the Web API application it's GPS location.  This is simply how web application work.

    Friday, March 13, 2020 9:31 PM
  • User-1852975005 posted

    Hang on a minute that was my original question.
    Were on a ,NET forum which is mainly C#, VB.
    In a sub section for API, APP.

    How do I get the coordinates from some ones mobile device.

    Thanks, 

    Saturday, March 14, 2020 7:35 AM
  • User475983607 posted

    Hang on a minute that was my original question.
    Were on a ,NET forum which is mainly C#, VB.
    In a sub section for API, APP.

    How do I get the coordinates from some ones mobile device.

    Thanks, 

    I think you misunderstand the web development fundamentals.  Web API is a server technology and runs on a web server not a mobile application.  The mobile application calls Web API and passes the location.  The mobile application can be a browser in which case see the link in my first post.  Otherwise you are writing a mobile application that user will install on their mobile device.

    The .NET System.Device.Location namespace has geo location APIs.  But remember you don't want to put this code in the Web API because it will always show the server's location not the mobile application.

    https://docs.microsoft.com/en-us/dotnet/api/system.device.location.geocoordinatewatcher?view=netframework-4.8

    Saturday, March 14, 2020 9:48 AM
  • User-1852975005 posted

    I have built a Web app.
    Which uses a browser to view whether on mobile phone or laptop.

    I have put this post in Web API, I can not see a section for Web App specifically.

    So, I can pass the coordinates from the mobile device to the Web App using the Java example?

    Saturday, March 14, 2020 10:56 AM
  • User475983607 posted

    NewToDotyNet

    So, I can pass the coordinates from the mobile device to the Web App using the Java example?

    Yes.  But the language is JavaScript not Java!  JavaScript is used to write code that runs in the browser; a browser application.   Please see the following code example whihc I copied from the link in my first post.

    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    <button id="find-me">Show my location</button>
    <br />
    <p id="status"></p>
    <a id="map-link" target="_blank"></a>
    
    
    @section scripts {
        <script>
            function geoFindMe() {
    
                const status = document.querySelector('#status');
                const mapLink = document.querySelector('#map-link');
    
                mapLink.href = '';
                mapLink.textContent = '';
    
                function success(position) {
                    const latitude = position.coords.latitude;
                    const longitude = position.coords.longitude;
    
                    status.textContent = '';
                    mapLink.href = `https://www.openstreetmap.org/#map=18/${latitude}/${longitude}`;
                    mapLink.textContent = `Latitude: ${latitude} °, Longitude: ${longitude} °`;
                }
    
                function error() {
                    status.textContent = 'Unable to retrieve your location';
                }
    
                if (!navigator.geolocation) {
                    status.textContent = 'Geolocation is not supported by your browser';
                } else {
                    status.textContent = 'Locating…';
                    navigator.geolocation.getCurrentPosition(success, error);
                }
    
            }
    
            document.querySelector('#find-me').addEventListener('click', geoFindMe);
        </script>
    }
    

    Use AJAX or a standard HTTP GET or POST to send the geo location to Web API.  The design depends on how your application works which is unknown at this time.

    Saturday, March 14, 2020 11:16 AM
  • User-1852975005 posted

    Yes sorry, JavaScript not Java.

    Which line forwards the location to the App?
    Just spotted text at bottom to answer this question..
    Use AJAX or a standard HTTP GET or POST to send the geo location to Web API.  The design depends on how your application works which is unknown at this time.

    Thanks,

    Saturday, March 14, 2020 11:21 AM
  • User1535942433 posted

    Hi NewToDotyNet,

    Which line forwards the location to the App?

    Accroding to your description,as far as I think,the getCurrentPosition() method is used to return the user's position.

    If the getCurrentPosition() method is successful, it returns a coordinates object to the function specified in the success(Position).The success() function outputs the Latitude and Longitude.

    Best regards,

    Yijing Sun

    Tuesday, March 17, 2020 7:48 AM