Skip to content

Instantly share code, notes, and snippets.

@nixta
Last active April 22, 2016 14:34
Show Gist options
  • Save nixta/45bc5f27d434bcd375d9a1e4e72a2345 to your computer and use it in GitHub Desktop.
Save nixta/45bc5f27d434bcd375d9a1e4e72a2345 to your computer and use it in GitHub Desktop.
Couple of suggestions for code
- (void)clickedonPoint {
self.m_mapView.touchDelegate = self;
self.m_sketchLayer.geometry = [[AGSMutablePoint alloc] initWithSpatialReference:self.m_mapView.spatialReference];
// Not sure why you have a sketchLayer here.
// However, if you do have one, you need to set the mapView's touchDelegate to the sketch layer.
// See https://developers.arcgis.com/ios/swift/guide/sketch-layer.htm#GUID-4CF8925A-B8B9-4656-AE3D-51DD126234CA
}
-(void)mapView:(AGSMapView *)mapView didClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features {
self.m_queryTask = [AGSQueryTask queryTaskWithURL:[NSURL URLWithString:CurrentLayerURL]];
// If the layer at CurrentLayerURL is already in the map, you don't need to query to get results, you can just
// get the features out of the "features" parameter that's being passed in. See 3_BestSuggestionIfLayerIsInMap.m
self.m_queryTask.delegate = self;
AGSQuery *currentQuery = [AGSQuery query];
currentQuery.spatialRelationship = AGSSpatialRelationshipIntersects;
currentQuery.outFields = @[@"*"];
currentQuery.returnGeometry = YES;
currentQuery.outSpatialReference = self.m_mapView.spatialReference;
AGSEnvelope *env = [AGSEnvelope envelopeWithXmin:mappoint.x - 10.0
ymin:mappoint.y - 10.0
xmax:mappoint.x + 10.0
ymax:mappoint.y + 10.0
spatialReference:self.m_mapView.spatialReference];
// I'm not sure what you're doing with WKIDs, but you should pass in a valid SpatialReference
currentQuery.geometry = env;
self.m_lastClickedMapPoint = mappoint; // Keep the search point for searching later.
[self.m_queryTask executeWithQuery:currentQuery];
}
-(void)queryTask:(AGSQueryTask *)queryTask operation:(NSOperation *)op didExecuteWithFeatureSetResult:(AGSFeatureSet *)featureSet {
if (featureSet.features > 0) {
NSArray *orderedFeatures = [featureSet.features sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
AGSGraphic *f1 = obj1, *f2 = obj2;
AGSGeometryEngine *ge = [AGSGeometryEngine defaultGeometryEngine];
double d1 = [ge distanceFromGeometry:self.m_lastClickedMapPoint toGeometry:f1.geometry];
double d2 = [ge distanceFromGeometry:self.m_lastClickedMapPoint toGeometry:f2.geometry];
return d1 < d2 ? NSOrderedAscending : (d1 > d2 ? NSOrderedDescending : NSOrderedSame);
}];
AGSGraphic *closestFeature = orderedFeatures[0];
// Do something with the closest feature here...
}
}
- (void)clickedonPoint {
self.m_mapView.touchDelegate = self;
}
-(void)mapView:(AGSMapView *)mapView didClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features {
self.m_queryTask = [AGSQueryTask queryTaskWithURL:[NSURL URLWithString:CurrentLayerURL]];
self.m_queryTask.delegate = self;
AGSQuery *currentQuery = [AGSQuery query];
currentQuery.spatialRelationship = AGSSpatialRelationshipIntersects;
currentQuery.outFields = @[@"*"];
currentQuery.returnGeometry = YES;
currentQuery.outSpatialReference = self.m_mapView.spatialReference;
AGSGeometry *searchGeom = [[AGSGeometryEngine defaultGeometryEngine] bufferGeometry:mappoint byDistance:10];
currentQuery.geometry = searchGeom;
self.m_lastClickedMapPoint = mappoint; // Keep the search point for searching later.
[self.m_queryTask executeWithQuery:currentQuery];
}
-(void)queryTask:(AGSQueryTask *)queryTask operation:(NSOperation *)op didExecuteWithFeatureSetResult:(AGSFeatureSet *)featureSet {
if (featureSet.features > 0) {
NSArray *orderedFeatures = [featureSet.features sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
AGSGraphic *f1 = obj1, *f2 = obj2;
AGSGeometryEngine *ge = [AGSGeometryEngine defaultGeometryEngine];
double d1 = [ge distanceFromGeometry:self.m_lastClickedMapPoint toGeometry:f1.geometry];
double d2 = [ge distanceFromGeometry:self.m_lastClickedMapPoint toGeometry:f2.geometry];
return d1 < d2 ? NSOrderedAscending : (d1 > d2 ? NSOrderedDescending : NSOrderedSame);
}];
AGSGraphic *closestFeature = orderedFeatures[0];
// Do something with the closest feature here...
}
}
- (void)clickedonPoint {
self.m_mapView.touchDelegate = self;
}
-(void)mapView:(AGSMapView *)mapView didClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features {
// Use the name of the layer from when it was added with AGSMapView:addMapLayer:withName
NSArray *featuresOnLayer = features[CurrentLayerName];
if (featuresOnLayer && featuresOnLayer.count > 0) {
// Now sort the features on order of distance from the map point.
NSArray *orderedFeatures = [featuresOnLayer sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
AGSGraphic *f1 = obj1, *f2 = obj2;
AGSGeometryEngine *ge = [AGSGeometryEngine defaultGeometryEngine];
double d1 = [ge distanceFromGeometry:mappoint toGeometry:f1.geometry];
double d2 = [ge distanceFromGeometry:mappoint toGeometry:f2.geometry];
return d1 < d2 ? NSOrderedAscending : (d1 > d2 ? NSOrderedDescending : NSOrderedSame);
}];
AGSGraphic *closestFeature = orderedFeatures[0];
// Do something with the closest feature here...
}
}
- (void)clickedonPoint {
self.m_mapView.touchDelegate = self;
}
-(void)printClickFeatures:(NSDictionary *)features {
for (NSString *layerName in features.allKeys) {
AGSLayer *layer = [self.m_mapView mapLayerForName:layerName];
NSURL *layerURL = nil;
if ([layer respondsToSelector:@selector(URL)]) {
layerURL = [layer performSelector:@selector(URL)];
}
NSArray *featuresForLayer = features[layerName];
NSLog(@"There are %lu features returned on layer '%@'. URL: %@", featuresForLayer.count, layerName, layerURL);
}
}
-(void)mapView:(AGSMapView *)mapView didClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features {
[self printClickFeatures:features];
// Use the name of the layer from when it was added with AGSMapView:addMapLayer:withName
NSArray *featuresOnLayer = features[CurrentLayerName];
if (featuresOnLayer && featuresOnLayer.count > 0) {
// Now sort the features on order of distance from the map point.
NSArray *orderedFeatures = [featuresOnLayer sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
AGSGraphic *f1 = obj1, *f2 = obj2;
AGSGeometryEngine *ge = [AGSGeometryEngine defaultGeometryEngine];
double d1 = [ge distanceFromGeometry:mappoint toGeometry:f1.geometry];
double d2 = [ge distanceFromGeometry:mappoint toGeometry:f2.geometry];
return d1 < d2 ? NSOrderedAscending : (d1 > d2 ? NSOrderedDescending : NSOrderedSame);
}];
AGSGraphic *closestFeature = orderedFeatures[0];
// Do something with the closest feature here...
}
}
@nixta
Copy link
Author

nixta commented Apr 22, 2016

I'm not sure why you are using a Sketch Layer. And I assume that the layer is already on the map. If that's the case, go with example 3 here.

I have tried to clean up some of your code in example 1.

In example 2, I still use the QueryTask, but use a buffer instead of an envelope.

However, if the layer with the features you want is on the map, use example 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment