检查标记是否在圆角AngularJS内

时间:2018-08-04 02:42:34

标签: angularjs google-maps ng-map

我想知道给定的标记是否在圆半径内。而且我想知道是否单击了标记,以便显示有关标记位置的警报。我正在使用ng-map。

Sample map image

我的HTML:

<html ng-app="myApp">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAHmXV5zem_Py_aFHAwPixEyjW1cV-gJ00&callback=initMap"type="text/javascript"></script>
<script src="https://code.angularjs.org/1.3.15/angular.js"></script>
<script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
</head>

<body ng-controller="MyController as vm">
  <br/>
  <br/>
  <br/>
  <ng-map zoom="11" center="{{vm.latlng}}" on-click="vm.setCenter(event)" tilt="0">
    <marker position="[-6.5829106488490865, 106.87462984179683]" on-click="vm.foo(2,3)" draggable="true"></marker>
    <shape name="circle" stroke-color='#FF0000' stroke-weight="2"
      center="{{vm.latlng}}" radius="{{vm.radius}}"
      on-click="vm.getRadius()"
      draggable="true"
      on-dragstart="vm.dragStart()"
      on-drag="vm.drag()"
      on-dragend="vm.dragEnd()"
      editable="true">
    </shape>
    <traffic-layer></traffic-layer>
  </ng-map>
</body>
</html>

我的控制器:

var app = angular.module('myApp', ['ngMap']);
app.controller('MyController', function(NgMap) {
  var map;
  var vm = this;
  NgMap.getMap().then(function(evtMap) {
    map = evtMap;
  });
  vm.latlng = [-6.584957, 106.804592];
  vm.radius = 5000;
  vm.getRadius = function(event) {
    alert('this circle has radius ' + this.getRadius());
    alert('Titik Tengah : ' + this.getCenter());
  }
  vm.setCenter = function(event) {
    console.log('event', event);
    map.setCenter(event.latLng); 
  }
  vm.foo = function(event, arg1, arg2) {
    alert('this is at '+ this.getPosition());
  }
  vm.dragStart = function(event) {
    console.log("drag started");
  }
  vm.drag = function(event) {
    console.log("dragging");
  }
  vm.dragEnd = function(event) {
    console.log("drag ended");
  }
});

谢谢

1 个答案:

答案 0 :(得分:1)

要确定圆圈中的标记是否来自geometry库中的google.maps.geometry.spherical.computeDistanceBetween function

先决条件

  

加载geometry库,例如:
  https://maps.google.com/maps/api/js?key=--YOUR KEY GOES HERE--&libraries=geometry

该示例演示了如何确定标记是否在区域(圆圈)内并使用其他图标进行渲染:

angular.module('mapApp', ['ngMap'])
    .controller('mapController', function ($scope, NgMap) {

        NgMap.getMap().then(function (map) {
            $scope.map = map;
        });

        $scope.center = [59.339025, 18.065818];
        $scope.radius = 500 * 1000; //in meters
       

        $scope.locations = [
            { id: 1, name: 'Oslo', pos: [59.923043, 10.752839]  },
            { id: 2, name: 'Stockholm', pos: [59.339025, 18.065818] },
            { id: 3, name: 'Copenhagen', pos: [55.675507, 12.574227] },
            { id: 4, name: 'Berlin', pos: [52.521248, 13.399038],  },
            { id: 5, name: 'Paris', pos: [48.856127, 2.346525] }
        ];

        let centerLatLng = new google.maps.LatLng($scope.center[0],$scope.center[1]);
        $scope.locations.forEach((loc,i) => {

            let pos  = new google.maps.LatLng(loc.pos[0],loc.pos[1]);
            if (google.maps.geometry.spherical.computeDistanceBetween(pos, centerLatLng) <= $scope.radius) {
                
                loc.icon = {"url": "http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png"};
            }  
        });


        

    });
<script src="https://maps.google.com/maps/api/js?libraries=geometry"></script>
<script src="https://code.angularjs.org/1.3.15/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">
    <ng-map default-style="true" zoom="4" center="{{center}}">
        <marker ng-repeat="l in locations" icon='{{l.icon}}' position="{{l.pos}}" title="{{l.name}}" id="{{l.id}}">
        </marker>
        <shape name="circle" stroke-color='#FF0000' stroke-weight="2" center="{{center}}" radius="{{radius}}" >
        </shape>
    </ng-map>
</div>