Esri / quickstart-map-ios

An iOS Framework and sample app to get started with Esri's iOS Runtime SDK.
http://resources.arcgis.com/en/help/runtime-ios-sdk/concepts/#//00pw00000003000000
Apache License 2.0
30 stars 22 forks source link

Convert mapview points to location coordinates #2

Closed sandeepgs1984 closed 10 years ago

sandeepgs1984 commented 11 years ago

Hi Nicholas,

How to convert screen points to location coordinates using AGSMapView?

There are couple of methods available in MKMapView for converting these:

    • (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view;
    • (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(UIView *)view;

Please tell me is there are any equivalent methods to achieve the same.

My context is that:

  1. Need to display annotations which are available in visible area of mapview and remove existing annotations as mapview is panned/zoomed, so that display annotations which are in visible area.
  2. For this, I am querying database using location coordinates to get the list of places in visible area.

This is my code: CLLocationCoordinate2D topCord = [self.mapView convertPoint:self.mapView.frame.origin toCoordinateFromView:self.mapView]; CLLocationCoordinate2D bottomCord = [self.mapView convertPoint:CGPointMake(self.mapView.frame.origin.x + self.mapView.frame.size.width, self.mapView.frame.origin.y + self.mapView.frame.size.height) toCoordinateFromView:self.mapView]; NSPredicate *pred = [NSPredicate predicateWithFormat:@"latitude <= %f AND longitude >= %f AND latitude >= %f AND longitude <= %f", topCord.latitude,topCord.longitude,bottomCord.latitude,bottomCord.longitude];

where self.mapView is kind of MKMapView. This is giving proper results.

How to achieve the same using ArcGIS API's?

Please help me in this scenario. Waiting for your kind reply.

Regards, Sandeep

nixta commented 11 years ago

Hi Sandeep.

Firstly, this is probably a much better question for the official ArcGIS Runtime for iOS forum, but I'm happy to help you here.

In your example, you are getting the top-left and bottom-right points in map coordinates. You can do this using the visibleAreaEnvelope property on AGSMapView. That will give you an AGSEnvelope in map coordinates (which is likely to be Web Mercator). So, you can then convert that to a Geographic coordinates envelope with this code. Assuming self.mapView points to your AGSMapView:

AGSEnvelope *mapCoordsExtent = self.mapView.visibleAreaEnvelope;
AGSEnvelope *latLonExtent = 
    [[AGSGeometryEngine defaultGeometryEngine] projectGeometry:mapCoordsExtent
                                            toSpatialReference:[AGSSpatialReference wgs84SpatialReference]];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"latitude <= %f AND longitude >= %f AND latitude >= %f AND longitude <= %f",
                        latLonExtent.ymax, latLonExtent.xmin,
                        latLonExtent.ymin, latLonExtent.xmax];

Note that if you want to translate Map coordinates in the AGSMapView to and from iOS Screen Coordinates, there are also methods on AGSMapView to do that for you:

For what it's worth, if I were doing this, I would probably create a category on AGSEnvelope to add a readonly property returning an NSPredicate :)

Let me know if this helps,

Nick.

sandeepgs1984 commented 11 years ago

Thanks Nick. I will let you know about this solution.

Regards, Sandeep

sandeepgs1984 commented 11 years ago

Hey Nick,

Its not exactly same as MKMapView co-ordinates. But its manageable.

AGSMapView coordinates---------> 51.330850 :: 8.978842 :: 48.330850 :: 13.046838 MKMapView coordinates----------> 50.715582 :: 9.338401 :: 48.158747 :: 12.777123

Thank You Very Much for your solution.

Regards, Sandeep

nixta commented 11 years ago

Glad I could help.

While the coordinates are close enough, I'm intrigued by the discrepancy. How are you setting the extent of the initial map for the AGSMapView (and likewise, how are you setting it with an MKMapView)?

Cheers,

Nick.

sandeepgs1984 commented 11 years ago

Hi Nick,

Here is the zoom extent for AGSMapView:

// Zooming to an initial envelope with the spatial reference of the map
float extentDelta = 1.5;
AGSEnvelope *envelope = [AGSEnvelope envelopeWithXmin:(11.012840 - extentDelta)
                                                 ymin:(49.83085 -  extentDelta)
                                                 xmax:(11.012840 + extentDelta)
                                                 ymax:(49.83085 +  extentDelta)
                                     spatialReference:[AGSSpatialReference wgs84SpatialReference]];
[self.arcGISMapView zoomToEnvelope:envelope animated:YES];

and for MKMapView: // Setting the initial region to map view MKCoordinateRegion region; region.center.latitude = 49.83085; region.center.longitude = 11.012840; region.span.latitudeDelta = 1.7; region.span.longitudeDelta = 2.3; [self.mapView setRegion:region];

Thanks, Sandeep

nixta commented 11 years ago

Thanks Sandeep.

Any reason you're using different widths and heights for MKMapView and AGSMapView code? That would explain some of the difference. You're also declaring a 3x3 degree minimal coverage area in the AGSMapView code and an coverage of 2.3x1.7 degrees for the MKMapView.

Nick.

sandeepgs1984 commented 11 years ago

There is no reason. I want to use same degrees used for MKMapView in AGSMapView. May be this will give correct results:

AGSEnvelope *envelope = [AGSEnvelope envelopeWithXmin:(11.012840 - 1.7) ymin:(49.83085 - 2.3) xmax:(11.012840 + 1.7) ymax:(49.83085 + 2.3) spatialReference:[AGSSpatialReference wgs84SpatialReference]]; [self.arcGISMapView zoomToEnvelope:envelope animated:YES];

Please correct me if I am wrong.

Thanks Sandeep.

nixta commented 11 years ago

Not quite. X is longitude, Y is latitude. Also (and I haven't used MKMapView) I think you would have to halve the deltas on AGSMapView. The MKMapView documentation seems to say the span is full width and height so when calculating edges around the center-point you'll want to use 1.15 and 0.85 instead of 2.3 and 1.7.

Like I said, I'm not familiar with MKMapView though.

Let me know if that works.

N