简体   繁体   English

Google Maps API,如何从机场代码获取时间?

[英]Google Maps API, how to get lat long from airport code?

I wonder how to get the LAT and LONG from an airport. 我想知道如何从机场获得LATLONG For example I have the airport code "CPH" (Copenhagen) and I need to draw a line from my marker to that point. 例如,我有机场代码"CPH" (Copenhagen)并且需要从标记到该点画一条线。 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). 使用地址解析器,它支持“ CPH”的缩写(为了安全起见,您可能希望将其设置为“ CPH机场”)。

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);
  }
});

working fiddle 工作小提琴

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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Maps API:分别获取经度/纬度 - Google maps API: get lat / long separately 使用谷歌地图 api,如何从纬度和经度获取国家、state 和城市? - using google maps api, how to get country, state, and city from lat and long? Google Maps API - 获取lat / long之间路线的点数 - Google Maps API - Get points along route between lat/long 使用Google Maps API获取经纬度的地址 - Get address with long and lat using google maps api 如何使用谷歌地图JS api从给定的纬度和经度坐标找到半径(比如10米)中的坐标(纬度和经度) - How to find the coordinates (lat & long) in a radius (say like 10 meters) from a given lat & long coordinate using google maps JS api 从自动完成地址输入中获取经纬度,并使用Google Maps API在下面显示地图 - Get Lat and Long from autocomplete address input and show the map below with google maps API 如何在Google Maps JS API中获取仍包含一组Lat / Long坐标的最小LatLngBounds? - How can I get the smallest LatLngBounds that still contains a set of Lat/Long Coordinates in Google Maps JS API? 如何从DirectionsResult对象中提取所有经纬度坐标(Google Maps Javascript路线API) - How to extract all lat-long coordinates from DirectionsResult object (Google Maps Javascript directions API) 如何获得谷歌地图上的移动对象的经时变化? - How to get Lat Long of moving object on google maps? 如何获得Google Maps Directions API以选择正确的机场? - How do I get Google Maps Directions API to choose the correct Airport?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM