Google maps: map does not render via call javascript / jQuery

0

I need to embed a map in my project.

However, from the tutorial of the API itself, I could not make it work, until they discovered that the problem was Bootstrap, or Laravel - or both, together. That is, although I am not sure, but under tests and tests, Laravel and Bootstrap do not combine with Google Maps.

And on the web there are many questions, but no answers that made me work.

The first approach was to use Javascript / jQuery to put the page with the Google Maps within an Iframe but did not work.

Trying another approach, it worked fine, but it did not serve me well, because I need the Javascript engine, my initial goal.

In this functional test, it looks like this:

1 - I created a php page with the Google tutorial code to display a simple map.

2 - The page name is googlemaps.php.

3 - The page has been created in the resources / views folder, and its content is

<?php
echo <<<BLOCO
<!--aqui vai o código html -->
<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      var map;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: -34.397, lng: 150.644},
          zoom: 8
        });
      }
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=MinhaChaveAPI=initMap"asyncdefer></script></body></html>BLOCO;?>

4-Imadearouteintheweb.phpfile,likethis:

Route::get("abrirGoogleMaps",function (){
return view('googlemaps');
});

5 - I typed in my project in development     http: // myusername / openGoogleMaps and everything worked perfectly.

Now, the problem:

When I try to call such a page through a Javascript / jQuery routine, it does not work.

For example, via jQuery, like this:

jQuery("#idIfrGoogleMaps").prop('src','googlemaps.php');

Inside the iFrame appears' Sorry, the page you are looking for could not be found. NotFoundHttpException in RouteCollection.php line 179: ' regardless of whether I point the full path, part of the path, return with '../ ..', anything.

The same happens if I replace the jQuery statement to use the created route:

jQuery.get("abrirGoogleMaps",function(){

});

If you open the Developer Area (F12) on the 'Network' tab, when you click on the route name 'openGoogleMaps', and also clicking on the 'Preview' tab, the complete 'googlemaps.php' page code appears, showing which route was called, that the page in the views folder was found, but there is of course no rendering of the map because of the message inside the iFrame.

    
asked by anonymous 30.07.2017 / 15:20

1 answer

1

As I've just discovered, to embed Google Maps in an Iframe, you need to have a special key because it's 'Google Maps embedded' type.

So, through the Developers link, you are asked to specific key for Iframe, and the code would look like this:

<iframe
  width="600"
  height="450"
  frameborder="0" style="border:0"
  src="https://www.google.com/maps/embed/v1/place?key=YOUR_API_KEY&q=Space+Needle,Seattle+WA" allowfullscreen>
</iframe>

Where, in the url where 'place' appears, this word can be replaced by directions, search, view or streetview .

(However, by changing this form of search - from 'place' to any others suggested - you will need another syntax, otherwise it will not work)

The word 'YOUR_API_KEY' is the required key to work, which is obtained through a button on that page of the link above, whose button has the label 'Get key'.

The desired location in the example is 'Space Needle, Seattle WA', whose spaces require either the plus sign (+) or the escape character '% 20', as shown in the example with the plus sign.

    
31.07.2017 / 13:55