2016-11-13 158 views
0

我是Angularjs的新手,我正在嘗試使用Angularjs處理谷歌地圖應用程序。除了一件事之外,我能夠產生預期的結果。當我將{{loc.co1}}打印爲表格列時。它沒有顯示結果並給出null。我搜查了很多,發現我的想法是正確的。結果在javascript中可用,但是當從html訪問時,它們不顯示。你能幫我解決這個問題嗎?無法獲得Angularjs在html頁面中顯示爲表達式的結果

輸入是當我們在地圖上繪製一個矩形並點擊提交時,座標值應該進入表格。

另外,下面是我做的工作的鏈接。對不起,沒有正確地遵循格式。

http://plnkr.co/edit/kh5cUJabvG2rPJUEbgyt?p=info

===========code =================== 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="ISO-8859-1"> 
<title>Scientist Entry Map</title> 
<link rel="stylesheet" href="ScientistMapPage.css"/> 

<script  src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script> 
<!-- Latest compiled and minified CSS --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 

<!-- Optional theme --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> 

<!-- Latest compiled and minified JavaScript --> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDVgNI2snXEm8Cu5y-dxk0Y1ep9Iv3SOC4&libraries=drawing,geometry,places" async defer></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script> 
<!-- <script type="text/javascript" src="lodash.min.js"></script> --> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script> 
<!-- <script src="https://code.angularjs.org/1.4.8/angular-route.min.js"></script> --> 
<!-- <script type="text/javascript" src="angular.min.js"></script> --> 
<!-- <script type="text/javascript" src="angular-google-maps.min.js"></script> --> 

<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-google-maps/2.3.3/angular-google-maps.min.js"></script> --> 

<script type="text/javascript" src="script.js"></script> 

<script type="text/javascript" src="ng-map.min.js"></script> 



</head> 
<body ng-app="myApp"> 

<div ng-controller="MapController" class="form-control"> 

<table> 
<tr><td><h1>Map for selecting places</h1></td></tr> 
<tr><td> 
<input id="placeSearch" class="form-control" type="text" placeholder="Search Box"> 

<div id="map"></div> 
<button class="btn btn-primary" type="reset" id="btnResetRect" placeholder="ResetMap">Reset Map</button> 
<button class="btn btn-primary" type="submit" id="btnSubmitRect">Submit Location</button> 
<input type="hidden" id="txtCoords"> 
    <input type="text" id="text" value="{{locationsData[0].co1}}"/> 

</td> 
<td> 


     <table class="table table-striped table-bordered"> 
      <tr> 
       <th>Place 
       </th> 
       <th>Coordinates-1 
       </th> 
       <th>Coordinates-2 
       </th> 
       <th>Coordinates-3 
       </th> 
       <th>Coordinates-4 
       </th> 
       <th>Delete</th> 
      </tr> 
      <tr ng-repeat="loc in locationsData"> 
       <td>to be added</td> 
       <td>{{loc.co1}}</td> 
       <td>{{loc.co2}}</td> 
       <td>{{loc.co3}}</td> 
       <td>{{loc.co4}}</td> 

       <td><button type="submit" class="btn btn-primary" id="deleteLocation">Delete</button></td> 

      </tr> 
     </table> 


</td></tr> 


</table> 

</div> 


</body> 
</html> 


// Code goes here 

var myApp = angular.module('myApp',[]); 

myApp.controller('MapController',function($scope) { 

$scope.rectShape; 
$scope.locationsData =[]; 

var mapOptions = { 
    zoom: 4, 
    center: new google.maps.LatLng(25, 80), 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 

$scope.map = new google.maps.Map(document.getElementById("map"), mapOptions); 




$scope.drawingMg = new google.maps.drawing.DrawingManager({ 
    drawingMode: google.maps.drawing.OverlayType.RECTANGLE, 
    drawingControl: true, 
    drawingControlOptions: { 
     position: google.maps.ControlPosition.TOP_CENTER, 

     drawingModes: [ 
      //google.maps.drawing.OverlayType.MARKER, 
      //google.maps.drawing.OverlayType.CIRCLE, 
      google.maps.drawing.OverlayType.POLYGON, 
      //google.maps.drawing.OverlayType.POLYLINE, 
      google.maps.drawing.OverlayType.RECTANGLE 
     ] 
    }, 

polygonOptions: { 
    fillColor: '#BCDCF9', 
     fillOpacity: 0.5, 
     strokeWeight: 2, 
     strokeColor: '#57ACF9', 
     clickable: false, 
     editable: true, 
     draggable : true, 
     zIndex: 1 
}, 
rectangleOptions: 
{ 
    fillColor: 'red', 
     fillOpacity: 0.5, 
     strokeWeight: 2, 
     strokeColor: 'red', 
     clickable: true, 
     editable: true, 
     draggable:true, 
     zIndex: 1 

} 
}); 

var c1,c2,c3,c4; 
google.maps.event.addListener($scope.drawingMg, 'rectanglecomplete', function(rectangle) { 
    //document.getElementById('info').innerHTML += "rectangle points:" + "<br>"; 

    $scope.rectShape= rectangle; 



    c1 = rectangle.getBounds().getNorthEast().lat() +"," +rectangle.getBounds().getNorthEast().lng(); 
    c2 = rectangle.getBounds().getNorthEast().lat()+"," +rectangle.getBounds().getSouthWest().lng(); 
    c3 = rectangle.getBounds().getSouthWest().lat()+"," +rectangle.getBounds().getNorthEast().lng(); 
    c4 = rectangle.getBounds().getSouthWest().lat()+"," +rectangle.getBounds().getSouthWest().lng(); 






    document.getElementById("txtCoords").innerHTML = c1 +"\n"+c2+"\n"+c3+"\n"+c4; 

    // alert(document.getElementById("txtCoords").innerHTML.toString()); 

    //document.getElementById("info").innerHTML += ne +"," +sw+ "<br>"; 

}); 




$scope.drawingMg.setMap($scope.map); 


$scope.clearButton = document.getElementById("btnResetRect"); 
$scope.map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push($scope.clearButton); 

$scope.submitButton = document.getElementById("btnSubmitRect"); 

$scope.submitButton.onclick = function(){ 

    alert(c1 +""+c2); 
    //$scope.locationsData =[]; 
    $scope.locationsData.push({ 
     co1 : c1, 
     co2: c2, 
     co3: c3, 
     co4: c4 
    }); 

    alert($scope.locationsData.length); 
    // $scope.locationsDatac1 = c1; 
    // $scope.locationsDatac2 = c2; 
    // $scope.locationsDatac3 = c3; 
    // $scope.locationsDatac4 = c4; 

    alert($scope.locationsData[0].co1); 


}; 



console.log("outside do click"); 



$scope.clearButton.onclick = function(){ 


    if($scope.drawingMg.getDrawingMode()){ 


     $scope.shape.setMap(null); 
     //$scope.shape.setBounds(null); 


     //$scope.drawingMg.getDrawingMode().setMap(null); 
    } 



}; 



$scope.placeSearch = document.getElementById("placeSearch"); 
$scope.searchBox = new google.maps.places.SearchBox($scope.placeSearch); 
$scope.map.controls[google.maps.ControlPosition.TOP_LEFT].push($scope.placeSearch); 

$scope.map.addListener('bounds_changed', function() { 
    $scope.searchBox.setBounds($scope.map.getBounds()); 
}); 

$scope.markers = []; 
// Listen for the event fired when the user selects a prediction and retrieve 
// more details for that place. 
$scope.searchBox.addListener('places_changed', function() { 
    $scope.places = $scope.searchBox.getPlaces(); 

    if ($scope.places.length == 0) { 
     return; 
    } 

    // Clear out the old markers. 
    $scope.markers.forEach(function (marker) { 
     marker.setMap(null); 
    }); 

    $scope.markers = []; 
    var center; 

    $scope.places.forEach(function (place) { 
     if (!place.geometry) { 
      console.log("Returned place contains no geometry"); 
      return; 
     } 
     var icon = { 
      url: place.icon, 
      size: new google.maps.Size(71, 71), 
      origin: new google.maps.Point(0, 0), 
      anchor: new google.maps.Point(17, 34), 
      scaledSize: new google.maps.Size(25, 25) 
     }; 

     // Create a marker for each place. 
     $scope.markers.push(new google.maps.Marker({ 
      map: $scope.map, 
      icon: icon, 
      title: place.name, 
      position: place.geometry.location 
     })); 

     if (place.geometry.viewport) { 
      // Only geocodes have viewport. 
      bounds = place.geometry.viewport; 
     } else { 
      bounds = place.geometry.location; 
     } 
    }); 
    $scope.map.fitBounds(bounds); 


}); 


}); 
+0

如何使這行得通?我應該給一個位置? – Sajeetharan

+0

是的。我應該從javascript中檢索位置座標以顯示在表格中。我感謝任何幫助。謝謝...... – Spandana

+0

嗨,我意識到,plnkr代碼沒有顯示輸出,並對其進行了一些更改,以便顯示地圖。另外,當我嘗試使用ng-model進行綁定時,它表示該指令不可用。任何人都可以幫助我解決這個問題。先謝謝你。 – Spandana

回答

1

您需要使用輸入ng-model

<input type="text" id="text" ng-model="location" /> 

我已經做了一些必要的修改,沒有它的工作原理

DEMO

+0

謝謝Sajeetharan ....這是現在的工作。我只是意識到我們需要在這種情況下的雙向綁定。 – Spandana

+0

@Spandana標記作爲答案,如果它有幫助 – Sajeetharan