google.maps.LatLng与google.maps.LatLngLiteral

时间:2019-02-06 02:52:22

标签: google-maps

我正在将google地图添加到我正在处理的应用程序中,并且成功地陷入了一个可能对我的应用程序没有任何影响的问题。

无论如何,我很好奇为什么Google Maps允许您使用     new google.maps.LatLng(x, y) 要么     {lat: x, lng: y}

我已经使用了这两种格式,但是绝对没有区别,但是我觉得好像我必须丢失一些东西……我不相信google.map.LatLng存在没有特定目的。

此:

function initMap() {
  var directionsService = new google.maps.DirectionsService();
  var directionsDisplay = new google.maps.DirectionsRenderer();
  var chicago = new google.maps.LatLng(41.850033, -87.6500523);
  var mapOptions = {
    zoom:7,
    center: chicago
  }

或者这个:

function initMap() {
  var directionsService = new google.maps.DirectionsService();
  var directionsDisplay = new google.maps.DirectionsRenderer();
  var chicago = {lat: 41.850033, lng: -87.6500523);
  var mapOptions = {
    zoom:7,
    center: chicago
  }

1 个答案:

答案 0 :(得分:0)

google.maps.LatLng是原始类(API的最早版本没有google.maps.LatLngLiteral),大多数但并非全部接受google.maps.LatLng的函数也接受{{ 3}}({lat: x, lng: y})。

  • google.maps.LatLngLiteral的优点是不需要加载API即可使用它,可以在加载API之前对其进行使用和初始化。

  • google.maps.LatLng的优点是某些功能(主要是google.maps.LatLngLiteral中的功能)仅接受google.maps.LatLng作为输入。

相关问题