BrianBunker / cmv-widgets

Widgets based on the Esri jsapi, configured for the Configurable Map Viewer. Widgets include Drag and Drop, Goto, Map Navigation Hash, and Nearby.
https://brianbunker.github.com/cmv-widgets
MIT License
18 stars 24 forks source link

Drive time polygon does not show on the map when custom map is used for a basemap (Nearby widget) #13

Closed darinatch closed 8 years ago

darinatch commented 9 years ago

Hi Brian, I am using our own basemaps and they have a spatial reference different than the ArcGIS online maps. When using the drive time functionality, the drive time polygon does not show on the map, because of the spatial reference differences.

I made some changes to Nearby widget to perform project on the resulting polygon, so it matches the map's spatial reference.

Below is the code, and if you think this might be of interest, I will do a pull request.

doNearbyAnalysis: function() {
            // which mode is selected? distance or drive time
            // get a geodesic circle or a drivetime polygon
            this.nearbyArea = null;
            if (this.nearbyMode === 'distance') {
                // what is the distance radius value?
                this.nearbyArea = new Circle({
                    center: this.pointGraphic.geometry,
                    geodesic: true,
                    radius: this.nearbyValueInput.get('value'),
                    radiusUnit: Units[this.nearbyModeDistance_options.get('value')]
                });
                this.selectNearbyFeatures();
                this.nearbyResultsNode.innerHTML = '';
            } else if (this.nearbyMode === 'drivetime') {
                // what is the drivetime radius value?
                this.drivetimeUI(true);
                request({
                    url: 'http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Network/USA/NAServer/Service%20Area/solveServiceArea',
                    content: {
                        facilities: '{"features": [{"geometry": ' + JSON.stringify(this.pointGraphic.geometry.toJson()) + '}]}',
                        defaultBreaks: this.nearbyValueInput.get('value'),
                        f: 'json'
                    },
                    handleAs: 'json',
                    callbackParamName: 'callback'
                }).then(lang.hitch(this, function(result) {
                    this.drivetimeUI(false);
                    // THIS IS THE CHANGE - added the if statement
                    if (result.saPolygons.spatialReference.wkid === this.map.spatialReference.wkid) {
                        this.nearbyArea = new Polygon(result.saPolygons.features[0].geometry);
                        this.selectNearbyFeatures();
                    } else {
                        this.projectDriveTimePolygon(result.saPolygons.features[0].geometry, result.saPolygons.spatialReference);
                    }
                }), lang.hitch(this, function(err) {
                    this.drivetimeUI(false);
                    this.nearbyResultsNode.innerHTML = 'Sorry, couldn\'t get drive time area. Try a shorter time or a location near a road. If the problem persists, the service might be down temporarily.';
                }));
            }
        },

projectDriveTimePolygon: function (polygonGeometry, inSR) {
            var outSR = new SpatialReference(this.map.spatialReference.wkid);
            var polygon = new Polygon(inSR);
            polygon.rings = polygonGeometry.rings;

            this.geometryService.project([polygon], outSR).then(lang.hitch(this, function (projectedPolygons) {
                this.nearbyArea = projectedPolygons[0];
                this.selectNearbyFeatures();
            }), lang.hitch(this, function (err) {
                this.nearbyResultsNode.innerHTML = 'Error occurred while reprojecting the drive time polygon';
            }));
        },
BrianBunker commented 8 years ago

@darinatch Yes! I am interested in a PR. Sorry for ignoring this issue for so long. I think when I saw this initially I thought I'd come back and review it at a better time, but then forgot. If you're still willing to do the PR, I'll merge it.