javascript - Google Map Condition on Town -
i want condition on code user input start point , end point, want make check on start point check located in london or not find code work in function want variable town make function outside of function create checkpoint.
var input = document.getelementbyid('start'); var autocomplete = new google.maps.places.autocomplete(input); autocomplete.bindto('bounds', map); var infowindow = new google.maps.infowindow(); // when user has clicked on autocomplete suggestion google.maps.event.addlistener(autocomplete, 'place_changed', function() { infowindow.close(); var place = autocomplete.getplace(); // town of selected place function gettown(address_components) { var geocoder = new google.maps.geocoder(); result = address_components; var info = []; (var = 0; < result.length; ++i) { if (result[i].types[0] == "locality") { return result[i].long_name; } } }; var town = gettown(place.address_components); // if place in london, move marker place if (town == 'london') { alert('in london'); } else { // if not, nothing , alert user alert('you must click on place in london'); } });
how can access var town outside of function on whole page make condition on base of it?
you can make variable outside of scope of callback set result to.
var input = document.getelementbyid('start'); var map = new google.maps.map(document.getelementbyid('map-canvas'), { zoom: 12, center: {lat: 51.507351, lng: -0.127758} }); var autocomplete = new google.maps.places.autocomplete(input); autocomplete.bindto('bounds', map); var town; // when user has clicked on autocomplete suggestion google.maps.event.addlistener(autocomplete, 'place_changed', function() { function gettown(address_components) { result = address_components; var info = []; (var = 0; < result.length; ++i) { if (result[i].types[0] == "locality") { return result[i].long_name; } } }; document.getelementbyid('place').innerhtml = ''; document.getelementbyid('town').innerhtml = ''; town = gettown(autocomplete.getplace().address_components); }); function inlondoncheck(placename) { document.getelementbyid('place').innerhtml = placename + " in london? " + (town === 'london'); document.getelementbyid('town').innerhtml = town || ''; } setinterval(function() { if (town) inlondoncheck(autocomplete.getplace().name); }, 500);
html, body, #map-canvas { height: 100%; margin: 0; padding: 0; }
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script> <input id="start"> <div>place<pre id="place"></pre></div> <div>town<pre id="town"></pre></div> <div id="map-canvas"></div>
Comments
Post a Comment