0

I need to place annotations according to their polar coordinate (with radius in pixels) originated at the user position. I use the following code snippet to do that.

CGPoint origin = [mapView convertCoordinate:userCoordinate toPointToView:mapView];

double dx = radius * cos(theta);
double dy = radius * sin(theta);
CGPoint point = CGPointMake(origin.x + dx, origin.y + dy);

NSLog(@"mapViewSize=(%f, %f), point=(%f,%f)", CGRectGetWidth(mapView.bounds), CGRectGetHeight(mapView.bounds), point.x, point.y);

return [mapView convertPoint:point toCoordinateFromView:mapView];

Here is an example of the NSLog output:

mapViewSize=(228.000000, 228.000000), point=(40.817908,123.346518)

In my opinion, this would mean that the point is indeed located on the map view. However, when adding them back on the map after convertPoint:point toCoordinateFromView:, it is placed very far (distance ~ 148847.221119 meters while the span of my region is just hundreds of meter) and therefore are out of the range of my map. What am I doing wrong?

user1553136
  • 328
  • 5
  • 17
  • How is `radius` calculated? If it's not based on the zoom level of the map, the distance won't scale correctly (10 screen pixels could be 10 km, 100 km, 1000 km, etc). Instead, if you know the distance in **meters** that you want to place the other annotations (you already know the bearing which is theta), then try [this](http://stackoverflow.com/a/6634982/467105) which deals directly with the natural units instead of screen units. –  Mar 27 '15 at 13:51
  • `radius` is a random number generated between 0 and the width of the mapView divided by two. The zoom is fixed. Therefore, in my understanding, it should appear on the map. – user1553136 Mar 27 '15 at 14:06

0 Answers0