【问题标题】:How do I change the style of a shape through the scope?如何通过范围更改形状的样式?
【发布时间】:2014-09-29 13:24:03
【问题描述】:

我正在使用自定义的GoogleMap AngularJS Directive,这是不太受欢迎的 GitHub 版本。我使用它是因为我对 other 处理事件的方式不满意。

我有一个充满地图形状的 GeoJSON 文件。我想让它具有交互性,以便您可以选择每个区域。所以我已经设法让初始区域选择和区域取消选择工作,但是如果你选择一个区域然后选择另一个区域,它应该改变旧选择区域的样式并更新新区域。

我遍历 GeoJSON 文件并创建范围。

$scope.shapes = [];
$http.get('/static/json/areas.json').then(function(res){
    for (var x = res.data.features.length -1; x >= 0; x--) {
        paths = [];
        for (var y = res.data.features[x].geometry.coordinates[0].length - 1; y >= 0; y--) {
            paths.push([ res.data.features[x].geometry.coordinates[0][y][3], res.data.features[x].geometry.coordinates[0][y][0]]);
        };
        $scope.shapes.push({
            areaName: res.data.features[x].properties.name,
            path: paths,
            fillColor: '#4F639E',
            fillOpacity: 0.4,
            strokeColor: '#4F639E',
            strokeOpacity: 1,
            strokeWeight:1
        });
    };
});

然后使用 ng-repeat 将它们添加到地图中。

<map
    zoom="13"
    center="[-33.955, 18.42]"
    disable-default-u-i="true"
    disable-double-click-zoom="true"
    draggable="true"
    keyboard-shortcuts="false"
    map-type-id="SATELLITE">
    <shape ng-repeat="shape in shapes track by $index"
        on-mouseover="areaOver()"
        on-mouseout="areaOut()"
        on-click="areaClick()"
        id="{{$index+1}}"
        name="polygon"
        stroke-color="{{shape.strokeColor}}"
        stroke-opacity="{{shape.strokeOpacity}}"
        stroke-weight="{{shape.strokeWeight}}"
        fill-color="{{shape.fillColor}}"
        fill-opacity="{{shape.fillOpacity}}"
        paths="{{shape.path}}">
    </shape>
</map>

然后我添加事件侦听器。 这两个用于悬停效果。

// Highlight area on hover
$scope.areaOver = function() {
    if ($scope.select != this.id) {
        this.setOptions({
            fillOpacity: 1
        });
    };
}

// Return area on mouse-out to previous style
$scope.areaOut = function() {
    if ($scope.select != this.id) {
        this.setOptions({
            fillOpacity: 0.4
        });
    };
}

当一个区域被选中时这个

$scope.areaClick = function() {
    var path = this.getPath();

    // The first time area selected
    if ($scope.select == null) {
        $scope.select = this.id;
        $scope.map.fitBounds(get_bounds(path.j))

        this.setOptions({
            fillOpacity: 0,
            strokeColor: '#FFFFFF',
            strokeWeight: 2,
            zIndex: +1
        });
    }

    // When the same area has been selected again - reset view.
    else if ($scope.select == this.id) {
        $scope.select = null;
        this.setOptions({
            fillOpacity: 0.4,
            strokeColor: '#4F639E',
            strokeWeight: 1,
            zIndex: -1
        });

        $scope.map.setZoom(13);
        $scope.map.setCenter({lat: -33.96, lng: 18.38});
    }

    // When a different area has been selected - reset old style then update new style.
    else {
        // Reset old selected shape
        $scope.map.data.revertStyle();  // Not working
        $scope.apply;

        for (var i = $scope.map.shapes.length - 1; i >= 0; i--) {
            $scope.map.shapes[i].setOptions({  // Not working
                fillOpacity: 0.4,
                strokeColor: '#4F639E',
                strokeWeight: 1,
                zIndex: -1
            });
        };
        $scope.apply;

        // Move / Style newly selected shape
        $scope.select = this.id;
        $scope.map.fitBounds(get_bounds(path.j))

        this.setOptions({
            fillOpacity: 0,
            strokeColor: '#FFFFFF',
            strokeWeight: 2,
            zIndex: +1
        });
    };
}

另外还有一个自定义函数来计算所选区域的中心。

// Custom Function
function get_bounds(path) {
    var smallest_lat = 360;
    var smallest_lng = 360;
    var largest_lat = -360;
    var largest_lng = -360;

    for (var i = path.length - 1; i >= 0; i--) {
        var lat = path[i].lat();
        var lng = path[i].lng();

        if (lat > largest_lat) {largest_lat = lat};
        if (lat < smallest_lat) {smallest_lat = lat};
        if (lng > largest_lng) {largest_lng = lng};
        if (lng < smallest_lng) {smallest_lng = lng};
    }

    var northEast = new google.maps.LatLng(smallest_lat, smallest_lng);
    var southWest = new google.maps.LatLng(largest_lat, largest_lng);
    var bounds = new google.maps.LatLngBounds(northEast, southWest);

    return bounds;
}

如何从范围内更新形状的样式? Plunker

【问题讨论】:

    标签: angularjs google-maps-api-3 angularjs-google-maps


    【解决方案1】:

    $scope.map.shapes[id]

    您在 angular-ui 谷歌地图上与我在同一页面上。 :)

    如果这不能满足你的问题,请根据例子做一个非常简单的plnkr,

    https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/shape.html 或者 https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/shape_with_ng_repeat.html

    那么,请告诉我。要创建 plnkr,只需点击页面上的“Plunkr”即可。

    我会修到你满意为止。

    【讨论】:

      猜你喜欢
      • 2020-10-27
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      相关资源
      最近更新 更多