I wonder how to get the LAT
and LONG
from an airport. For example I have the airport code "CPH" (Copenhagen)
and I need to draw a line from my marker to that point. Right now my code for the line is this:
var flightPlanCoordinates = [
new google.maps.LatLng(event.latLng.lat(), event.latLng.lng()),
new google.maps.LatLng(21.291982, -157.821856)
];
flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 1
});
flightPath.setMap(map);
But I need the line to go the the airport. I've looked at the geocoding but I don't understand on how to use it with an airport. Can someone do a example for me? Thank you! :)
Use the geocoder, it supports the "CPH" abbreviation (you might want to make it "CPH airport" to be safe).
geocoder.geocode( { 'address': "CPH airport"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
CPH = results[0].geometry.location;
bounds.extend(CPH);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
code snippet:
var geocoder = new google.maps.Geocoder(); var map; var CPH; var bounds = new google.maps.LatLngBounds(); function initialize() { map = new google.maps.Map( document.getElementById("map_canvas"), { center: new google.maps.LatLng(37.4419, -122.1419), zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP }); geocoder.geocode({ 'address': "CPH airport" }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { CPH = results[0].geometry.location; bounds.extend(CPH); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); } else { alert('Geocode was not successful for the following reason: ' + status); } }); google.maps.event.addListener(map, 'click', function(event) { var flightPlanCoordinates = [ event.latLng, CPH ]; bounds.extend(event.latLng); flightPath = new google.maps.Polyline({ path: flightPlanCoordinates, geodesic: true, strokeColor: '#FF0000', strokeOpacity: 1.0, strokeWeight: 1 }); map.fitBounds(bounds); flightPath.setMap(map); }); } google.maps.event.addDomListener(window, "load", initialize);
html, body, #map_canvas { height: 500px; width: 500px; margin: 0px; padding: 0px }
<script src="https://maps.googleapis.com/maps/api/js"></script> <div id="map_canvas" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.