2013-10-04 57 views
5

我試圖將簡單的Google Maps項目(從他們的網站上的示例完成)遷移到AngularJS項目。讓我先說這是因爲我對AngularJS和web dev都是新手,所以請客氣:)我有一個簡單的項目,在https://github.com/eroth/angular-web-app(主要文件如下),我使用Google Maps教程從初始提交開始試圖將其轉換爲AngularJS應用程序,但一直未能得到它的工作,雖然我已經驗證我的MapController.js文件中的$scope.init()函數正在從我的map.html文件調用。嘗試使用簡單的Google Maps示例與AngularJS配合使用

我的問題有兩方面:是我的代碼有問題,還是我需要類似的東西(看起來非常好):http://nlaplante.github.io/angular-google-maps/?我正在將其合併到我的另一個分支的項目中,但我試圖弄清楚它是否是我現有代碼的問題,或者如果我需要類似這個庫的東西來使它與AngularJS一起工作。如果是後者,爲什麼?提前道歉,如果它是我犯的一些非常簡單的錯誤;正如我所說的,我對這一切都非常陌生,儘管我經歷了一些AngularJS教程,看起來很棒。

這是我map.html文件:

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset = "utf-8"> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 

    <!-- INCLUDE REQUIRED THIRD PARTY LIBRARY JAVASCRIPT AND CSS --> 
    <script type="text/javascript" src="js/angular.min.js"></script> 
    <link rel="stylesheet" href="css/bootstrap.min.css"> 
    <link rel="stylesheet" href="css/bootstrap-responsive.min.css"> 

    <!-- INCLUDE APPLICATION SPECIFIC CSS AND JAVASCRIPT --> 
    <script type="text/javascript" 
     src="http://maps.googleapis.com/maps/api/js?sensor=true&language=en"> 
    </script> 
    <script type="text/javascript" src="js/web-app/app.js"></script> 
    <script type="text/javascript" src="js/web-app/controllers/mainController.js"></script> 
    <link rel="stylesheet" href="css/main.css"> 
    </head> 
    <body> 
    <div class="container main-frame" ng-app="WebApp" ng-controller ="mainController" ng-init="init()"> 
     <div id="map-canvas"/> 
    </div> 
    </body> 
</html> 

這是我MapController.js文件:

app.controller("mainController", function($scope, $http){ 
$scope.initialLocation; 
$scope.siberia = new google.maps.LatLng(60, 105); 
$scope.newyork = new google.maps.LatLng(40.7463, -73.9913); 
$scope.browserSupportFlag = new Boolean(); 
$scope.map; 
$scope.retina = window.devicePixelRatio > 1; 

$scope.init = function() { 
    var mapOptions = { 
     zoom: 13, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

    console.log('In main init'); 

    //first test——map uses hard-coded location, next will get user's location and pull deals 
    $scope.map = new google.maps.Map(document.getElementById("map-canvas"), 
     mapOptions); 

    // Try W3C Geolocation (Preferred) 
    if (navigator.geolocation) { 
     $scope.browserSupportFlag = true; 
     navigator.geolocation.getCurrentPosition(function(position) { 
     $scope.initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     var currentLat = position.coords.latitude; 
     var currentLon = position.coords.longitude; 
     $scope.map.setCenter($scope.initialLocation); 
     // performApiCall(currentLat, currentLon); 

     //definite custom map pin for user location & plot it on map 
     var pinColor = "5ea9ff"; 
     var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.6|0|" + pinColor + "|0|_|k", 
     new google.maps.Size(25, 60), 
     new google.maps.Point(0,0), 
     new google.maps.Point(10, 24)); 
     var marker = new google.maps.Marker({ 
      position: $scope.initialLocation, 
      map: $scope.map, 
      icon: pinImage, 
     }); 
     }, function() { 
     handleNoGeolocation($scope.browserSupportFlag); 
     }); 
    } 

    // Browser doesn't support Geolocation 
    else { 
     $scope.browserSupportFlag = false; 
     handleNoGeolocation($scope.browserSupportFlag); 
    } 

    function handleNoGeolocation(errorFlag) { 
     if (errorFlag == true) { 
     alert("Geolocation service failed."); 
     $scope.initialLocation = newyork; 
     } else { 
     alert("Your browser doesn't support geolocation. We've placed you in Siberia."); 
     $scope.initialLocation = siberia; 
     } 
     map.setCenter($scope.initialLocation); 
    } 
}; 

google.maps.event.addDomListener(window, 'load', $scope.init); 
}); 

提前任何幫助謝謝!

回答

5

你的代碼有幾個錯誤。首先,您已經通過在DOM中使用ng-init來定義您的示波器的init()函數,您的mainController已經調用了該函數的init()函數。

<div class="container main-frame" ng-app="WebApp" ng-controller ="mainController" ng-init="init()"> 

這意味着你不需要監聽器窗口中加載調用$scope.init - 因爲這會運行函數兩次,並反過來初始化地圖的兩倍。刪除監聽器。其次,有幾個實例調用變量,它們實際上是$ scope對象的一部分(newyork Ln 57,serbia Ln 60和map Ln 62) - 因此會拋出未定義的錯誤。

最後,您必須在CSS中爲地圖的div容器設置高度,以便實際查看地圖。

#map-canvas { height : 200px; } 
/* if you want to set the height as a percentage of the parent div, remember to give a height to the parent div as well */ 

至於你的問題的第二部分,我一定會考慮使用已經建成的東西。我沒有使用過你鏈接的圖書館,但是如果你說它非常好 - 爲什麼要重新發明輪子?

相關問題