Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert mapview points to location coordinates #2

Closed
sandeepgs1984 opened this issue May 31, 2013 · 8 comments
Closed

Convert mapview points to location coordinates #2

sandeepgs1984 opened this issue May 31, 2013 · 8 comments

Comments

@sandeepgs1984
Copy link

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
Copy link
Member

nixta commented May 31, 2013

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:

  • - (AGSPoint *) toMapPoint: (CGPoint) point; (documentation here) and
  • - (CGPoint) toScreenPoint: (AGSPoint *) point; (documentation here)

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
Copy link
Author

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

Regards,
Sandeep

@sandeepgs1984
Copy link
Author

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
Copy link
Member

nixta commented Jun 3, 2013

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
Copy link
Author

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
Copy link
Member

nixta commented Jun 4, 2013

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
Copy link
Author

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
Copy link
Member

nixta commented Jun 5, 2013

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

@nixta nixta closed this as completed Mar 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants