Google Maps - Displaying Location

244 views
Skip to first unread message

Greg J

unread,
Nov 14, 2016, 5:09:34 PM11/14/16
to DroidScript
Hi...Am a long time premium member and fully support this app. Love it...my shout out...smiles.

I am trying to display my google map location using the Google map API and sample code. I am getting a location error because chrome/google maps doesn't allow location services from localhost (file:///). The map paints in droidscript but I receive: "Error: The Geolocation service failed". I do have location settings turned on for the device. I have seen the "allow all access" setting but deem this dangerous ("C:\PathTo\Chrome.exe" --allow-file-access-from-files)

I can setup a nanoHttpd server but this seems extreme. 
Can I use WebView in some way? 
Any suggestions?

Note: The Google API key is one I scraped from their sample page: https://developers.google.com/maps/documentation/javascript/geolocation

<!DOCTYPE html>
<html>
  <head>
    <title>Geolocation</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
    
      
      
      
      // Note: This example requires that you consent to location sharing when
      // prompted by your browser. If you see the error "The Geolocation service
      // failed.", it means you probably did not give permission for the browser to
      // locate you.

      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -34.397, lng: 150.644},
          zoom: 6
        });
        var infoWindow = new google.maps.InfoWindow({map: map});

        // Try HTML5 geolocation.
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var pos = {
              lat: position.coords.latitude,
              lng: position.coords.longitude
            };

            infoWindow.setPosition(pos);
            infoWindow.setContent('Location found.');
            map.setCenter(pos);
          }, function() {
            handleLocationError(true, infoWindow, map.getCenter());
          });
        } else {
          // Browser doesn't support Geolocation
          handleLocationError(false, infoWindow, map.getCenter());
        }
      }

      function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        infoWindow.setPosition(pos);
        infoWindow.setContent(browserHasGeolocation ?
                              'Error: The Geolocation service failed.' :
                              'Error: Your browser doesn\'t support geolocation.');
      }
    </script>
    <script async defer
    </script>
  </body>
</html>

Dave Smart

unread,
Nov 15, 2016, 6:43:56 AM11/15/16
to androi...@googlegroups.com
Hi Greg,

This may not be the best way to solve your problem, but it's the first one that comes to mind:-  You could run a local web server inside your app with just a few lines of code and serve up the files to your webview from that server.  This is what I did when I created the downloadable 'Stencil HTML Game' demo in DroidScript.  First try with http://localhost  and if that does not work then get the local IP address using app.GetIPAddress() and use that.

This is a neat trick you can use to make 3rd party JavaScript libraries work when running locally.  Especially if they make use of Ajax calls with the xmlhttprequest object.

Regards
David
Reply all
Reply to author
Forward
0 new messages