ani
ani

Reputation: 245

Get map size on zoom using Mapbox iOS SDK

I have two view. 1 loaded mapbox view another map image. Now I want to calculate map position on clicking a position in image.

But I not getting exact position. I assume map position that I am taking is wrong.

I am calculating this way

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

       CGPoint location=[[touches anyObject] locatinInView:self];

       CGPoint point;
       point.x=(location.x * self.mapView.mapScrollView.contentSize.width)/self.size.width);
       point.y=(location.y * self.mapView.mapScrollView.contentSize.height)/self.size.height);

       mapView.mapScrollView.contentOffset=point;

}

P.N -- I have tried in a alternate way also but not working. It's giving perfect when you will zoom upto 4 but after that it's not giving exact position

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

       CGPoint location=[[touches anyObject] locatinInView:self];

       CGPoint point;
       point.x=(location.x * self.mapView.projection.planetBounds.size.width/self.mapView.metersPerPixel)/self.size.width);
       point.y=(location.y * self.mapView.mapScrollView.projection.planetBounds.size.height/self.mapView.metersPerPixel)/self.size.height);

       mapView.mapScrollView.contentOffset=point;

}

Any idea how to do this?

I am using Mapbox sdk.

https://www.mapbox.com/

Upvotes: 0

Views: 1237

Answers (2)

zhocker
zhocker

Reputation: 173

MapBox iOS SDK has this delegation method

-(void)singleTapOnMap:(RMMapView *)mapView at:(CGPoint)point;

RMMapView has method

  • (CLLocationCoordinate2D)pixelToCoordinate:(CGPoint)pixelCoordinate

Hint : [mapView pixelToCoordinate:(CGPoint)];

and

- (CGPoint)coordinateToPixel:(CLLocationCoordinate2D)coordinate

Hint : [mapView coordinateToPixel:coordinate];

Hope it help you.

Upvotes: 0

incanus
incanus

Reputation: 5128

Don't go at the map view's scroll view directly -- it's private API (despite being open source). Take a look at the methods here instead for conversions:

https://www.mapbox.com/mapbox-ios-sdk/api/#//api/name/coordinateToPixel:

Upvotes: 1

Related Questions