2015-07-21 177 views
0

我有標記的負載增加谷歌地圖的標記設置爲中心,角谷歌地圖標記

但是,當用戶改變縮放或向右移動左邊的地圖,標記位置變化和標記移動到中心和以前不一樣,我加在頁面載入COORDS的

這是我的代碼:

<ui-gmap-google-map center="map.center" zoom="map.zoom" draggable="false" options="options"> 
     <ui-gmap-marker idkey="'1'" coords='map.center' 
     icon='{url: "//developers.google.com/.../beachflag.png" }'> 
      <ui-gmap-window isiconvisibleonclick="true"> 
          <p ng-cloak>Marker Clicked!</p> 
      </ui-gmap-window> 
     </ui-gmap-marker> 
</ui-gmap-google-map> 

這是我的地圖對象

enter image description here

我的問題一樣在這裏: https://stackoverflow.com/questions/31113161/angular-google-map-marker-position-not-fixed-moving-when-map-moves-how-to-make

如何添加標記是固定的地方,如果用戶向左移動向右或縮放地圖上相同的原始座標中標記的住宿。

感謝

回答

3

你的問題是,ui-gmap-markercoords屬性是map.center參考,當您移動地圖這改變。您需要爲標記擁有自己的控制器變量,因此它具有自己的位置屬性。事情是這樣的,

控制器:

// Map initialization 
$scope.map = { center: { latitude: 32.0889, longitude: 34.8357 }, zoom: 16}; 

// Give the marker its own scope variable, you can attach other info here, too 
$scope.marker = {coords: angular.copy($scope.map.center)} 

HTML:

<ui-gmap-google-map center="map.center" zoom="map.zoom" draggable="false" options="options"> 
    <ui-gmap-marker idkey="'1'" coords='marker.coords' 
     icon='{url: "//developers.google.com/.../beachflag.png" }'> 
     <ui-gmap-window isiconvisibleonclick="true"> 
      <p ng-cloak>Marker Clicked!</p> 
     </ui-gmap-window> 
    </ui-gmap-marker> 
</ui-gmap-google-map> 

編輯:忘angular.copy第一。增加了顯示破碎和工作行爲的強盜。

破碎:http://plnkr.co/edit/nkecrOXm84V6lEVr85Yi?p=preview

工作:http://plnkr.co/edit/kvgLSeuKTPo6FKqIFE89?p=preview

1
This code shows multiple marker in google map. 



<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script> 
<script src="https://code.angularjs.org/1.4.5/angular.js"></script> 
<script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script> 

<div ng-app="mapApp" ng-controller="mapController"> 

    <map zoom-to-include-markers="true" style="display:block; width:900px; height:500px;"> 
     <marker ng-repeat="pin in markerData" position="{{pin.latitude}},{{pin.longitude}}" title="{{pin.title}}" on-click="showDetail(pin)"></marker> 
     <info-window id="foo-iw"> 
      <div ng-non-bindable=""> 
       <h1>{{currentPin.title}}</h1><br/> 
       <a href="{{currentPin.url}}">Click Here</a> 
      </div> 

     </info-window> 
    </map> 
</div> 

<script> 
angular.module('mapApp', ['ngMap']) 
    .controller('mapController', function (NgMap, $scope, $q, $log) { 

     NgMap.getMap().then(function(map) { 
      $scope.map = map; 
      //$scope.map.setZoom(13); 
      var latlng = new google.maps.LatLng(37.0902, -122.636652); 
      $scope.map.setCenter(latlng); 
     }); 

     $scope.currentPin = {title: "",url:""}; 
     $scope.markerData = []; 
     $scope.cityMetaData = []; 

     $scope.getCityInfo = function (country) { 
      $scope.markerData = []; 

      var data = [ 
       { cityName: ' Petaluma, CA, USA','url':'https://www.flipkart.com/' }, 
       { cityName: ' Jackson Heights, Queens, NY, USA','url':'https://www.flipkart.com/' }, 
       { cityName: ' UNICEF 6 Fairbridge Avenue Belgravia, Harare, Zimbabwe','url':'https://www.flipkart.com/' }, 
      ]; 


      data.forEach(function (item) { 
       var cityData = item; 
       $scope.cityMetaData.push(cityData); 
       $scope.addressMarker(cityData); 
      }); 
     } 


     $scope.showDetail = function (e, pin) { 
      $scope.currentPin = pin; 
      $scope.map.showInfoWindow('foo-iw', this); 
     }; 

     $scope.hideDetail = function() { 
      $scope.map.hideInfoWindow('foo-iw'); 
     }; 

     $scope.addressMarker = function (cityItem) { 
      var deferred = $q.defer(); 
      var address = cityItem.cityName; 
      var url = cityItem.url; 
      var geocoder = new google.maps.Geocoder(); 
      geocoder.geocode({ 
       'address': address 
      }, function (results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        $scope.$apply(function() { 
         $scope.markerData.push({ 
          "latitude": results[0].geometry.location.lat(), 
          "longitude": results[0].geometry.location.lng(), 
          "title": results[0].formatted_address 
          ,"url": url 
         }); 
        }); 
       } else { 
        $log.info('Geocode was not successful for the following reason:' + status); 
       } 
      }); 
     } 
     $scope.getCityInfo(); 




    }); 
</script>