Request user location

Eduardo Gomez 3,651 Reputation points
2023-06-20T10:41:06.5133333+00:00

Hello

I was using Microsoft Geolocation class, but is not very accuarate (using desktop)

So what I want to do, is to open a WebView2 inside my app, and request the location and capture the latitude and longitude. If the user press deny, I want to show an error in a content dialog.

is there any way to do this?

I am using winUI

Windows development | Windows App SDK
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,681 Reputation points
    2023-06-20T12:01:18.4033333+00:00

    I am using winUI

    You don't need a WebView2 control.

    For example, this test in WinUI 3 works for me :

                    HttpWebRequest httpWebRequest = ((HttpWebRequest)(WebRequest.Create("https://ipapi.co/json")));
                    httpWebRequest.Method = "GET";
                    httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
                    httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "txt");
                    httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv: 52.0) Gecko/20100101 Firefox/52.0 SeaMonkey/2.49.4";
                    WebResponse webResponse = httpWebRequest.GetResponse();
    
                    Stream stream = webResponse.GetResponseStream();
                    var streamReader = new StreamReader(stream);
                    string sResp = streamReader.ReadToEnd();
    
                    string sBeginLatitude = "\"latitude\": ";
                    string sEndLatitude = ",";
                    string sLatitude = sResp.Substring(sResp.IndexOf(sBeginLatitude) + sBeginLatitude.Length);
                    sLatitude = sLatitude.Substring(0, sLatitude.IndexOf(sEndLatitude));
    
                    string sBeginLongitude = "\"longitude\": ";
                    string sEndLongitude = ",";
                    string sLongitude = sResp.Substring(sResp.IndexOf(sBeginLongitude) + sBeginLongitude.Length);
                    sLongitude = sLongitude.Substring(0, sLongitude.IndexOf(sEndLongitude));
    
                    Debug.WriteLine("Latitude : " + sLatitude);
                    Debug.WriteLine("Longitude : " + sLongitude);
    
                    webResponse.Close();
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.