2016-11-23 3 views
-1
<!DOCTYPE html> 
<html> 
    <head> 
    <title>PlaceID finder</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <script src="~/Scripts/jquery-3.1.0.min.js"></script> 
    <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; 
     } 
     .controls { 
     background-color: #fff; 
     border-radius: 2px; 
     border: 1px solid transparent; 
     box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3); 
     box-sizing: border-box; 
     font-family: Roboto; 
     font-size: 15px; 
     font-weight: 300; 
     height: 29px; 
     margin-left: 17px; 
     margin-top: 10px; 
     outline: none; 
     padding: 0 11px 0 13px; 
     text-overflow: ellipsis; 
     width: 400px; 
     } 

     .controls:focus { 
     border-color: #4d90fe; 
     } 
    </style> 
    </head> 
    <body> 
    <input id="pac-input" class="controls" type="text" 
     placeholder="Enter a location"> 
    <div id="map"></div> 
<input style="margin-left:5px" type="button" class="btn btn-primary" value="Add Store Point" id="addNewStore" /> 

    <script> 
     // This sample uses the Place Autocomplete widget to allow the user to search 
     // for and select a place. The sample then displays an info window containing 
     // the place ID and other information about the place that the user has 
     // selected. 

     // This example requires the Places library. Include the libraries=places 
     // parameter when you first load the API. For example: 
     // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"> 

     function initMap() { 
     var map = new google.maps.Map(document.getElementById('map'), { 
      center: {lat: -33.8688, lng: 151.2195}, 
      zoom: 13 
     }); 

     var input = document.getElementById('pac-input'); 

     var autocomplete = new google.maps.places.Autocomplete(input); 
     autocomplete.bindTo('bounds', map); 

     map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 

     var infowindow = new google.maps.InfoWindow(); 
     var marker = new google.maps.Marker({ 
      map: map 
     }); 
     marker.addListener('click', function() { 
      infowindow.open(map, marker); 
     }); 

     autocomplete.addListener('place_changed', function() { 
      infowindow.close(); 
      var place = autocomplete.getPlace(); 
      if (!place.geometry) { 
      return; 
      } 

      if (place.geometry.viewport) { 
      map.fitBounds(place.geometry.viewport); 
      } else { 
      map.setCenter(place.geometry.location); 
      map.setZoom(17); 
      } 

      // Set the position of the marker using the place ID and location. 
      marker.setPlace({ 
      placeId: place.place_id, 
      location: place.geometry.location 
      }); 
      marker.setVisible(true); 

      infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + 
       'Place ID: ' + place.place_id + '<br>' + 
       place.formatted_address); 
      infowindow.open(map, marker); 
     }); 
     } 

$("#addNewStore").on("click", function() { 
      //i need these variables 
      //...=place.name; 
      //...=place.place_id; 
      //...=place.formatted_address; 

      /$.ajax({ 
       url: '/Store/Add', 
       type: 'POST', 
       data: { 
        PlaceName:place.name,PlaceID:place.place_id, Address:place.formatted_address 
       }, 
       success: function (results) { 
        alert("Successfully added new store"); 
       }, error: function (data) { 
        alert("failed to add store"); 
       } 
      }); 
     }); 
    </script> 
    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap" 
     async defer></script> 
    </body> 
</html> 

Das Beispiel ist von Google Maps-Dokumentation und ich finde es sehr nützlich für mich. Mein Problem ist, dass ich den InfoWindow-Inhalt nicht in meiner anonymen Funktion verwenden kann, die der Button aufruft. Ich habe mit 2 Möglichkeiten der Konstruktorfunktionen versucht, aber ich habe es nicht herausgefunden. Vielen Dank im Voraus ^^Google Maps - InfoWindow Inhalt

+0

Was ist Ihre Frage? Haben Sie versucht, die Infowindow-Variable global zu machen? – geocodezip

+0

Ja, ich versuche, alle infoWindow Inhalte als verschiedene Variablen zu bekommen, wenn es möglich ist. –

+0

Wenn Sie 'place' Objekt außerhalb der' initMap' verwenden möchten, machen Sie es global. z.B. vor der 'function initMap()' set 'var place;' Und erinnere dich an das Entfernen der 'var' von' var place = autocomplete.getPlace(); ' – MKiss

Antwort

0

Mein Lehrer zeigte mir einen einfachen Weg, wie es geht.

In den Körper sollten Sie Div hinzufügen, ohne etwas Sichtbares darin. Nachdem Sie die Markierung so eingestellt haben, dass sie sichtbar ist, erhalten Sie die Klasse .hidden-data und Sie können für jede Eigenschaft der Markierung so viele Attribute hinzufügen, wie Sie möchten.

<div class="hidden-data" data-place="" data-location="" data-address=""></div> 
       marker.setVisible(true); 

       $(".hidden-data").attr('data-place', place.name); 
       $(".hidden-data").attr('data-location', place.geometry.location); 
       $(".hidden-data").attr('data-address', place.formatted_address); 

Die jQuery-Funktion, die auf Tastenklick aufgerufen werden sollte wie folgt aussieht:

$("#addNewStore").click(function() { 
    var a = $(".hidden-data").attr('data-place'); 
    var a = $(".hidden-data").attr('data-location'); 
    var a = $(".hidden-data").attr('data-address'); 
}); 
Verwandte Themen