2016-06-01 2 views
-1

Finden lat & lng von einem Marker, mit der automatischen Vervollständigung Suchfeld

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title>GLANCE</title> 
 
    <meta charset="utf-8" /> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
 

 
    <style> 
 
    html, 
 
    body { 
 
     height: 100%; 
 
     margin: 0; 
 
     padding: 0; 
 
    } 
 
    #map { 
 
     height: 70%; 
 
    } 
 
    .controls { 
 
     margin-top: 10px; 
 
     border: 1px solid transparent; 
 
     border-radius: 2px 0 0 2px; 
 
     box-sizing: border-box; 
 
     -moz-box-sizing: border-box; 
 
     height: 32px; 
 
     outline: none; 
 
     box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3); 
 
    } 
 
    #pac-input { 
 
     background-color: #000000; 
 
     font-family: Roboto; 
 
     font-size: 15px; 
 
     font-weight: 300; 
 
     margin-left: 12px; 
 
     padding: 0 11px 0 13px; 
 
     text-overflow: ellipsis; 
 
     width: 300px; 
 
    } 
 
    #pac-input:focus { 
 
     border-color: #4d90fe; 
 
    } 
 
    .pac-container { 
 
     font-family: Roboto; 
 
    } 
 
    #type-selector { 
 
     color: #000000; 
 
     background-color: #4d90fe; 
 
     padding: 5px 11px 0px 11px; 
 
    } 
 
    #type-selector label { 
 
     font-family: Roboto; 
 
     font-size: 13px; 
 
     font-weight: 300; 
 
    } 
 
    #target { 
 
     width: 345px; 
 
    } 
 
    </style> 
 
</head> 
 

 
<body> 
 
    <input id="pac-input" class="controls" type="text" placeholder="Digita Località"> 
 
    <div id="map"></div> 
 
    <script> 
 
    // This example adds a search box to a map, using the Google Place Autocomplete 
 
    // feature. People can enter geographical searches. The search box will return a 
 
    // pick list containing a mix of places and predicted search terms. 
 

 
    // 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"> 
 
    var marker; 
 
    var xlat; 
 
    var xlon; 
 

 
    function initAutocomplete() { 
 
     var map = new google.maps.Map(document.getElementById('map'), { 
 
     center: { 
 
      lat: 44.689205, 
 
      lng: 10.663778 
 
     }, 
 
     zoom: 14, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
     }); 
 
     if (navigator.geolocation) { 
 
     navigator.geolocation.getCurrentPosition(function(position) { 
 
      initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
 
      map.setCenter(initialLocation); 
 
      xlat = position.coords.latitude; 
 
      xlon = position.coords.longitude; 
 

 
      marker = new google.maps.Marker({ 
 
      icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png', 
 
      map: map, 
 
      draggable: true, 
 
      animation: google.maps.Animation.DROP, 
 
      position: { 
 
       lat: position.coords.latitude, 
 
       lng: position.coords.longitude 
 
      } 
 

 
      }); 
 
      marker.addListener('click', toggleBounce); 
 
     }); 
 

 

 
     } 
 

 
     // Create the search box and link it to the UI element. 
 
     var input = document.getElementById('pac-input'); 
 
     var searchBox = new google.maps.places.SearchBox(input); 
 
     map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 
 

 
     // Bias the SearchBox results towards current map's viewport. 
 
     map.addListener('bounds_changed', function() { 
 
     searchBox.setBounds(map.getBounds()); 
 
     }); 
 

 

 
     var markers = []; 
 
     // Listen for the event fired when the user selects a prediction and retrieve 
 
     // more details for that place. 
 
     searchBox.addListener('places_changed', function() { 
 
     var places = searchBox.getPlaces(); 
 

 
     if (places.length == 0) { 
 
      return; 
 
     } 
 

 
     // Clear out the old markers. 
 
     markers.forEach(function(marker) { 
 
      marker.setMap(null); 
 
     }); 
 
     markers = []; 
 

 
     // For each place, get the icon, name and location. 
 
     var bounds = new google.maps.LatLngBounds(); 
 
     places.forEach(function(place) { 
 

 

 
      var icon = { 
 
      url: place.icon, 
 
      size: new google.maps.Size(71, 71), 
 
      origin: new google.maps.Point(0, 0), 
 
      anchor: new google.maps.Point(17, 34), 
 
      scaledSize: new google.maps.Size(25, 25) 
 
      }; 
 

 
      // Create a marker for each place. 
 
      markers.push(new google.maps.Marker({ 
 
      map: map, 
 
      draggable: true, 
 
      animation: google.maps.Animation.DROP, 
 
      position: place.geometry.location 
 
       // var xlat = place.geometry.location.lat(); 
 
       // var xlon = place.geometry.location.lon(); 
 
      })); 
 

 

 

 

 
      if (place.geometry.viewport) { 
 
      // Only geocodes have viewport. 
 
      bounds.union(place.geometry.viewport); 
 
      } else { 
 
      bounds.extend(place.geometry.location); 
 
      } 
 
     }); 
 
     map.fitBounds(bounds); 
 
     }); 
 
    } 
 

 

 
    function vai() { 
 
     window.location = "listadomande.jsp?lat=" + xlat + "&lon=" + xlon; 
 
    } 
 
    </script> 
 
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDho6YqRH605Poc-S0AcShQu4hLYDUMGpk&libraries=places&callback=initAutocomplete" async defer></script> 
 
    <footer id="footer"> 
 

 
    <br> 
 
    <ul class="actions"> 
 
     <li> 
 
     <p onclick="vai()" class="button big">INVIA</p> 
 
     </li> 
 
    </ul> 
 
    </footer> 
 

 
</body> 
 

 
</html>

es mir gelingt, die Koordinaten des Anfangspunktes zu finden, die ich xlat & XLON genannt habe (geolocalized), aber ich don‘ Ich weiß, wie ich Xlat und Xlon ändere, wenn ich einen anderen Ort suche. Ich habe versucht mit xlat = place.geometry.location.lat(); xlon = place.geometry.location.lon(); Aber wenn ich setzen diese die Karte verschwinden = ( Link to the map page

`var marker; 
    var xlat; 
    var xlon; 
     function initAutocomplete() { 
     var map = new google.maps.Map(document.getElementById('map'), { 
      center: {lat: 44.689205, lng: 10.663778}, 
      zoom: 14, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function (position) { 
       initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
       map.setCenter(initialLocation); 
       xlat = position.coords.latitude; 
       xlon = position.coords.longitude; 

       marker = new google.maps.Marker({ 
        icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png', 
        map: map, 
        draggable: true, 
        animation: google.maps.Animation.DROP, 
        position: {lat: position.coords.latitude, lng: position.coords.longitude} 

       }); 
        marker.addListener('click', toggleBounce); 
      }); 


      } 

     // Create the search box and link it to the UI element. 
     var input = document.getElementById('pac-input'); 
     var searchBox = new google.maps.places.SearchBox(input); 
     map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 

     // Bias the SearchBox results towards current map's viewport. 
     map.addListener('bounds_changed', function() { 
      searchBox.setBounds(map.getBounds()); 
     }); 


     var markers = []; 
     // Listen for the event fired when the user selects a prediction and retrieve 
     // more details for that place. 
     searchBox.addListener('places_changed', function() { 
      var places = searchBox.getPlaces(); 

      if (places.length == 0) { 
      return; 
      } 

      // Clear out the old markers. 
      markers.forEach(function(marker) { 
      marker.setMap(null); 
      }); 
      markers = []; 

      // For each place, get the icon, name and location. 
      var bounds = new google.maps.LatLngBounds(); 
      places.forEach(function(place) { 


      var icon = { 
       url: place.icon, 
       size: new google.maps.Size(71, 71), 
       origin: new google.maps.Point(0, 0), 
       anchor: new google.maps.Point(17, 34), 
       scaledSize: new google.maps.Size(25, 25) 
      }; 

      // Create a marker for each place. 
      markers.push(new google.maps.Marker({ 
       map: map, 
       draggable: true, 
       animation: google.maps.Animation.DROP, 
       position: place.geometry.location 
      **xlat = place.geometry.location.lat(); 
      xlon = place.geometry.location.lon();** 
      })); 




      if (place.geometry.viewport) { 
       // Only geocodes have viewport. 
       bounds.union(place.geometry.viewport); 
      } else { 
       bounds.extend(place.geometry.location); 
      } 
      }); 
      map.fitBounds(bounds); 
     }); 
} 


    function go(){ 
     window.location="get.jsp?lat="+xlat+"&lon="+xlon; 
    }` 
+0

Können Sie bitte Geige dafür erstellen? –

+0

@NiketanRaval Ich legte das Snippet und auch den Link zu der Seite unter der Beschreibung des Problems ... Danke !! – Marco

Antwort

0

Ich denke, was hier eine Lösung sein kann, ist nicht xlat und XLong als Variablen innerhalb der Javascript-Code zu setzen, sondern sie als versteckte Elemente auf der Seite zu schließen und die Werte festgelegt, wenn Sie die geografische Breite und Länge zurück. So zum Beispiel in Ihrem hTML-Code, legen Sie so etwas wie diese

<div id="xlat" data-attr-val="" ></div> 
<div id="xlong" data-attr-val="" ></div> 

Dann, wenn Sie die Längen- und Breitengrad-Werte zurückgeben, können Sie diese Zeilen ersetzen

**xlat = place.geometry.location.lat(); 
xlon = place.geometry.location.lon();** 

mit:

Dann, wenn Sie die Funktion vai() aufrufen möchten können Sie dies

function vai() { 
    lat = document.getElementById('xlat').getAttribute("data-attr-val"); 
    lng = document.getElementById('xlng').getAttribute("data-attr-val"); 
    window.location = "listadomande.jsp?lat=" + lat + "&lon=" + lng; 
} 
+0

Danke für die Antwort, aber es verschwindet immernoch, wenn ich 'document.getElementById ('xlat') benutze. SetAttribute (" data-attr-val ", place.geometry.location.lat()); document.getElementById ('xlong'). setAttribute ("daten-attr-val", place.geometry.location.lon()); 'Innerhalb der Erstellung des Markers denke ich, dass ich es vielleicht woanders hinlegen muss, aber weiß nicht wo .. – Marco

+0

Können Sie sehen, ob die Attribute richtig eingestellt wurden? –

+0

für den ursprünglichen Speicherort ja, aber das Suchfeld funktioniert nicht, wenn ich diese zwei Zeilen innerhalb 'markers.push (new google.maps.Marker ({...}))' 'So kann ich nicht überprüfen, ob die Attribute haben die richtigen Werte ... – Marco

0

Es gibt mit diesem Code ein paar Probleme verwenden. Betrachten Sie bitte dieses Teil:

markers.push(new google.maps.Marker({ map: map, draggable: true, animation: google.maps.Animation.DROP, position: place.geometry.location var xlat = place.geometry.location.lat(); var xlon = place.geometry.location.lon(); }));

  1. Sie versuchen, Variablen xlat und XLON innerhalb des Objekts zu definieren, die Sie als Marker Optionen übergeben. Sie sollten diese Variablen außerhalb des Objekts platzieren.
  2. Außerdem haben Sie die Variablen xlat und xlon außerhalb der Funktion initAutocomplete() definiert, so dass Sie var nicht erneut verwenden müssen.
  3. LatLng Klasse von Karten JavaSript API nicht lon() Methode hat, hat es lng() Methode: https://developers.google.com/maps/documentation/javascript/reference?hl=en#LatLng

So sollte Ihr Code

markers.push(new google.maps.Marker({ map: map, draggable: true, animation: google.maps.Animation.DROP, position: place.geometry.location })); xlat = place.geometry.location.lat(); xlon = place.geometry.location.lng();

Sie werden modifizierte Version bei http://jsbin.com/nojima/edit?html,output sehen

Verwandte Themen