2014-12-21 44 views
0

我使用以下jsBin example

的默認值內$scope.position並給予當有人進入輸入框的位置並點擊了按鈕,谷歌地圖運行檢查對於該位置並返回位置的新緯度和長度,我想要的是pre用新的緯度和經度值更新。

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

function appCtlr($scope, $http, $timeout) { 
    var map, marker, mapOptions, myLatlng; 

    $scope.output = function(lat, lng) { 
     $scope.codeoutput = "var map;\nfunction initialize() {\n var myLatlng = new google.maps.LatLng(" + lat + "," + lng + ");\n var mapOptions = {\n zoom: 4,\n center: myLatlng\n }\n\n var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);\n\n var marker = new google.maps.Marker({\n position: myLatlng,\n map: map\n });\n}\n\ngoogle.maps.event.addDomListener(window, 'load', initialize);\n\n<div id=\"map_canvas\"></div>"; 
    }; 

    function initialize() { 
     $scope.position = {}; 
     $scope.position.lat = -25.363882; 
     $scope.position.lng = 131.044922; 
     myLatlng = new google.maps.LatLng($scope.position.lat, $scope.position.lng); 
     mapOptions = { 
      zoom: 4, 
      center: myLatlng 
     }; 
     map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); 

     marker = new google.maps.Marker({ 
      position: myLatlng, 
      map: map 
     }); 
     $scope.output($scope.position.lat, $scope.position.lng); 

    } 


    $scope.reposition = function() { 
     url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + $scope.location + '&sensor=false'; 

     $http.get(url, null, function(data) { 
      if (data.status != "ZERO_RESULTS") { 
       var p = data.results[0].geometry.location; 
       $scope.position.lat = p.lat; 
       $scope.position.lng = p.lng; 
       latlng = new google.maps.LatLng(p.lat, p.lng); 
       marker.setPosition(latlng); 
       map.panTo(latlng); 
      } 

      $scope.output($scope.position.lat, $scope.position.lng); 
      console.log($scope.position); 
      console.log($scope.codeoutput); 
     }); 
    }; 

    initialize(); 
} 

回答

1

你需要調用$scope.output()success回調

$http.get(url, null, function(data) { 

    }).success(function(data) { 

     if (data.status != "ZERO_RESULTS") { 
      var p = data.results[0].geometry.location; 
      $scope.position.lat = p.lat; 
      $scope.position.lng = p.lng; 
      latlng = new google.maps.LatLng(p.lat, p.lng); 
      marker.setPosition(latlng); 
      map.panTo(latlng); 
     } 

     $scope.output($scope.position.lat, $scope.position.lng); 

    }); 

Updated Bin

+0

我已經做了和jsbin作品,但我的地方例如不,我有' console.log($ scope.codeoutput)'它顯示我需要的東西,但DOM不會更新 – ngplayground

+0

我懷疑它,我將'$ scope.codeoutput'更改爲'$ rootScope.c odeoutput' – ngplayground