gree / unity-webview

zlib License
2.25k stars 687 forks source link

can`t get geolocation #996

Closed zenk1n closed 10 months ago

zenk1n commented 10 months ago

thank you for your hard work!

I have embedded your plugin and everything is fine. through it, I open a site with maps, but it says that the user's geolocation cannot be determined.

I can't determine the geolocation of the ceres WebView user? or do I need to add some lines of code

KojiNakamaru commented 10 months ago

For Android

You need to declare a permission in AndroidManifest.xml. By opening "Project Settings/Android/Publish Setings" and check "Custom Main Manifest", you can generate Assets/Plugins/Android/AndroidManifest.xml. Then, please add the following:

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

You also have to request the permission at runtime, for example:

#if UNITY_ANDROID
        if (!Permission.HasUserAuthorizedPermission("android.permission.ACCESS_FINE_LOCATION"))
        {
            var permissions = new string[]
            {
                "android.permission.ACCESS_FINE_LOCATION"
            };
            var callbacks = new PermissionCallbacks();
            callbacks.PermissionGranted += (permission) =>
            {
            };
            callbacks.PermissionDenied += (permission) =>
            {
            };
            callbacks.PermissionDeniedAndDontAskAgain += (permission) =>
            {
            };
            Permission.RequestUserPermissions(permissions, callbacks);
        }
#endif

Then the following should work.

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Geolocation Demo</title>
    <script>
      window.addEventListener(
        'load',
        function() {
          if ('geolocation' in navigator) {
            navigator.geolocation.getCurrentPosition(
              function(position) {
                console.log(position);
                document.getElementById('msg').innerHTML = '[' + position.coords.latitude + ', ' + position.coords.longitude + ']';
              },
              function(error) {
                console.log(error);
              },
              {
                enableHighAccuracy: true,
                timeout: 50000,
                maximumAge: 0,
              }
            );
          }
        }
      );
    </script>
  </head>
  <body>
    <div id="msg">[]</div>
  </body>
</html>

For iOS

You need to add NSLocationWhenInUseUsageDescription (Privacy - Location When In Use Usage Description) in Info.plist.

zenk1n commented 10 months ago

thank you, i have declared a permission

but I want to open an Online Map using WebView and for the user's geolocation to be displayed correctly there is this possible?

KojiNakamaru commented 10 months ago

Do you also request the permission at runtime? Just declaring the permission in AndroidManifest.xml is not enough. If you do everything in https://github.com/gree/unity-webview/issues/996#issuecomment-1773848162 , you can display google map correctly.

zenk1n commented 10 months ago

Oh, yeah! I`ve done literally 5 min ago. Thank you so much!