2016-03-01 41 views
5

我打算在我的應用程序之一中使用谷歌地圖「containsLocation()」API。文檔鏈接是 - https://goo.gl/4BFHCz谷歌地圖錯誤 - a.lng不是函數

我遵循相同的示例。這是我的代碼。

<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <title>Polygon arrays</title> 
    <style> 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 
     #map { 
     height: 100%; 
     } 
    </style> 
    </head> 
    <body> 
    <div id="map"></div> 
    <script> 
     // This example requires the Geometry library. Include the libraries=geometry 
     // parameter when you first load the API. For example: 
     // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry"> 

     function initMap() { 
     var map = new google.maps.Map(document.getElementById('map'), { 
      //center: {lat: 24.886, lng: -70.269}, 
      center: {lat: 12.9629277, lng: 77.7178972}, 
      zoom: 30, 
     }); 

     /*var triangleCoords = [ 
      {lat: 25.774, lng: -80.19}, 
      {lat: 18.466, lng: -66.118}, 
      {lat: 32.321, lng: -64.757} 
     ];*/ 

     var triangleCoords = [ 
      {lat: 12.96301273, lng: 77.71785952}, 
      {lat: 12.96314857, lng: 77.71784072}, 
      {lat: 12.96316124, lng: 77.71784037}, 
      {lat: 12.96295465, lng: 77.71788993}, 
      {lat: 12.96293329, lng: 77.7179345}, 
     ]; 

     var bermudaTriangle = new google.maps.Polygon({paths: triangleCoords}); 

     google.maps.event.addListener(map, 'click', function(e) { 

      var curPosition = {"lat":12.9629277,"lng":77.7178972}; 
      //var curPosition = e.latLng; 

      console.log("e content : "+JSON.stringify(e.latLng)); 
      console.log("curPosition content : "+JSON.stringify(curPosition)); 

      var resultColor = 
       google.maps.geometry.poly.containsLocation(curPosition, bermudaTriangle) ? 
       'red' : 
       'green'; 

      new google.maps.Marker({ 
      position: curPosition, 
      map: map, 
      icon: { 
       path: google.maps.SymbolPath.CIRCLE, 
       fillColor: resultColor, 
       fillOpacity: .2, 
       strokeColor: 'white', 
       strokeWeight: .5, 
       scale: 10 
      } 
      }); 


     }); 
     } 
    </script> 
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCiAur6ANJsV776iVmMDJcHYjQkXrXyu8s&libraries=geometry&callback=initMap" 
     async defer></script> 
    </body> 
</html> 

如果您看到我創建了一個curPosition變量,它包含latLng數據。我的問題在這裏只要var curPosition = e.latLng;它工作正常,但是,當我將其更改爲var curPosition = {"lat":12.9629277,"lng":77.7178972};它開始顯示錯誤"Uncaught TypeError: a.lng is not a function"

無法理解爲什麼會發生這種情況?任何線索......?

CONSOLE.LOG截屏附 enter image description here

問候

+0

可能有助於張貼你在那裏有控制檯日誌。尤其是JSON.stringify(e.latLng) – user2263572

+2

嘗試傳遞** google.maps.LatLng **對象:'var curPosition = new google.maps.LatLng(12.9629277,77.7178972);' –

+0

@ user2263572:console.log截圖隨附 – mi6crazyheart

回答

8

containsLocation方法需要google.maps.LatLng作爲「點」(至少在存在),則不能使用一個谷歌.lpsLatLngLiteral爲currentLocation

the documentation:

containsLocation(點:經緯度,多邊形:多邊形)|返回值:布爾值

計算給定點是否位於指定的多邊形內。

var curPosition = new google.maps.LatLng(12.9629277,77.7178972); 

代碼片段:

function initMap() { 
 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
    center: { 
 
     lat: 12.9629277, 
 
     lng: 77.7178972 
 
    }, 
 
    zoom: 30, 
 
    }); 
 

 
    var triangleCoords = [ 
 
    {lat: 12.96301273,lng: 77.71785952}, {lat: 12.96314857, lng: 77.71784072}, {lat: 12.96316124, lng: 77.71784037}, {lat: 12.96293329, lng: 77.7179345}, {lat: 12.96295465, lng: 77.71788993}]; 
 
    var bermudaTriangle = new google.maps.Polygon({ 
 
    paths: triangleCoords, 
 
    map: map 
 
    }); 
 

 
    var curPosition = new google.maps.LatLng(12.9629277, 77.7178972); 
 
    console.log("curPosition content : " + JSON.stringify(curPosition)); 
 

 
    var resultColor = 
 
    google.maps.geometry.poly.containsLocation(curPosition, bermudaTriangle) ? 
 
    'red' : 
 
    'green'; 
 

 
    new google.maps.Marker({ 
 
    position: curPosition, 
 
    map: map, 
 
    icon: { 
 
     path: google.maps.SymbolPath.CIRCLE, 
 
     fillColor: resultColor, 
 
     fillOpacity: .2, 
 
     strokeColor: 'white', 
 
     strokeWeight: .5, 
 
     scale: 10 
 
    } 
 
    }); 
 
    var curPositionB = new google.maps.LatLng(12.963, 77.71788993); 
 
    var resultColorB = google.maps.geometry.poly.containsLocation(curPositionB, bermudaTriangle) ? 
 
    'red' : 
 
    'green'; 
 

 
    new google.maps.Marker({ 
 
    position: curPositionB, 
 
    map: map, 
 
    icon: { 
 
     path: google.maps.SymbolPath.CIRCLE, 
 
     fillColor: resultColorB, 
 
     fillOpacity: .2, 
 
     strokeColor: 'white', 
 
     strokeWeight: .5, 
 
     scale: 10 
 
    } 
 
    }); 
 
} 
 
google.maps.event.addDomListener(window, 'load', initMap);
html, 
 
body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
#map { 
 
    height: 100%; 
 
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script> 
 
<div id="map"></div>

+1

是的,我知道'''google.maps.LatLngLiteral'''不能用於獲取當前位置。爲了獲得當前位置,我將使用其他技術。但是,我的要求是一旦我得到當前位置,我將如何知道特定位置是否在該Polygon的內部。 – mi6crazyheart