0

I'd like to use google maps in an unconventional way. I want to execute the javacript but I do not want to render any images. Rather I want to send some data to a file, use some of the computational functions the API has to offer and then pass back a text response. I attempted this with the following code but I then realized this probably won't work as javascript is run in a browser and not as a remote web service. That being said, how could a remote server call a 'web service' much like the one my code below has to offer with the goal of getting the simple 'yes' or 'no' response.

<?php

  // Get vars
  $lat=$_GET["lat"];
  $lon=$_GET["lon"];

?>



<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?key=MYKEYNOTYOURS:)&sensor=true&libraries=geometry">
    </script>
    <script type="text/javascript">
// Poygon getBounds extension - google-maps-extensions
// http://code.google.com/p/google-maps-extensions/source/browse/google.maps.Polygon.getBounds.js
if (!google.maps.Polygon.prototype.getBounds) {
  google.maps.Polygon.prototype.getBounds = function(latLng) {
    var bounds = new google.maps.LatLngBounds();
    var paths = this.getPaths();
    var path;

    for (var p = 0; p < paths.getLength(); p++) {
      path = paths.getAt(p);
      for (var i = 0; i < path.getLength(); i++) {
        bounds.extend(path.getAt(i));
      }
    }

    return bounds;
  }
}

// Polygon containsLatLng - method to determine if a latLng is within a polygon
google.maps.Polygon.prototype.containsLatLng = function(latLng) {
  // Exclude points outside of bounds as there is no way they are in the poly
  var bounds = this.getBounds();

  if(bounds != null && !bounds.contains(latLng)) {
    return false;
  }

  // Raycast point in polygon method
  var inPoly = false;

  var numPaths = this.getPaths().getLength();
  for(var p = 0; p < numPaths; p++) {
    var path = this.getPaths().getAt(p);
    var numPoints = path.getLength();
    var j = numPoints-1;

    for(var i=0; i < numPoints; i++) { 
      var vertex1 = path.getAt(i);
      var vertex2 = path.getAt(j);

      if (vertex1.lng() < latLng.lng() && vertex2.lng() >= latLng.lng() || vertex2.lng() < latLng.lng() && vertex1.lng() >= latLng.lng())  {
        if (vertex1.lat() + (latLng.lng() - vertex1.lng()) / (vertex2.lng() - vertex1.lng()) * (vertex2.lat() - vertex1.lat()) < latLng.lat()) {
          inPoly = !inPoly;
        }
      }

      j = i;
    }
  }

  return inPoly;
}


      function initialize() {


        var mapOptions = {
          center: new google.maps.LatLng(38.990842,-76.93625),
          zoom: 17,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

      var box = [new google.maps.LatLng(38.9913160,-76.937079), 
      new google.maps.LatLng(38.991333,-76.936119),
      new google.maps.LatLng(38.990287, -76.936108),
      new google.maps.LatLng(38.990278,-76.937057),
      new google.maps.LatLng(38.990495,-76.937052), 
      new google.maps.LatLng(38.990499,-76.936424),
      new google.maps.LatLng(38.991091,-76.93643),
      new google.maps.LatLng(38.991104,-76.937079)
      ];

        var mPoint = [new google.maps.LatLng(38.991300,-76.936165)];

        var AVpoly = new google.maps.Polygon({path:box,
           strokeColor:"#0000FF",
           strokeOpacity:0.8,
           strokeWeight:2,
           fillColor:"#0000FF",
           fillOpacity:0.4});

        var lat = <?php echo $lat; ?>;
        var lon = <?php echo $lon; ?>;

if(AVpoly.containsLatLng(new google.maps.LatLng(lat,lon), box) == true) {
        document.write("Yes");
}
else
{
document.write("No");
}

      }



    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>
WildBill
  • 9,143
  • 15
  • 63
  • 87
  • google map it self sends json response as web service , so what exactly is your requirement – Hussain Akhtar Wahid 'Ghouri' Dec 07 '12 at 06:44
  • How would I set up a a google map page such that a web-client (non-browser, think php-server like) can make the request with a POST and only get a 'Yes' or 'No' response back. This might be in fact very easy but I'm not seeing how to transform the above page into one that only returns a simple string - or a simple JSON object and not the entire page. Is this a possible answer - http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php? – WildBill Dec 07 '12 at 07:15

1 Answers1

2

This is not allowed! You may only use Google Maps Data if you are displaying a map that is publicly accessible. See section 9.1 of the TOS and 10.1.1 (g) "No Use of Content without a Google Map.":

Marcelo
  • 9,387
  • 3
  • 35
  • 40
  • absolute , contented and sufficient – Hussain Akhtar Wahid 'Ghouri' Dec 07 '12 at 08:24
  • Also I'm becoming humbled by realizing javascript and the google maps api are things to be run on the client and not the server side. I'm not sure this is technically feasible either... – WildBill Dec 07 '12 at 16:18
  • @WildBill the API itself is Javascript, therefore to be run on the client, but there are also various related services that you can query from the server side. Still, the data obtained from those services is to be used in conjunction with a Google map. – Marcelo Dec 07 '12 at 16:25
  • Yes, the thing to do is 'find another way' when working wit geoprocessing. I wanted to find if a point was within a polygon or not. I ended up finding some python code that performs a ray casting algorithm and essentially does the same thing but only on the server side... – WildBill Dec 07 '12 at 16:57
  • @WildBill You can do that with a spatial database server like PostgreSQL/PostGIS, and it would be all server side, ...and legal.;-) http://postgis.refractions.net/documentation/ – Marcelo Dec 07 '12 at 17:02
  • agreed that postgres is the 'better' way to go and will be the implementation for any large scale/operational use. Currently I'm working a prototype and thus a small amount of space within a single python program will suffice. Thanks for the input! – WildBill Dec 07 '12 at 17:08