Convert longitude/latitude to x/y on iPhone
i am showing a image in an UIImageView and i’d like to convert coordinates to x/y values so i can show citys on this image.
This is what i tried based on my research:
CGFloat height = mapView.frame.size.height; CGFloat width = mapView.frame.size.width; int x = (int) ((width/360.0) * (180 + 8.242493)); //Main lon int y = (int) ((height/180.0) * (90 - 49.993615)); //Mainz lat NSLog(@"x:%i y:%i",x,y); PinView *pinView = [[PinView alloc]initPinViewWithPoint:x andY:y]; [self.view addSubview:pinView];
which gives me 167 as x and y=104 but this example should have the values x=73 & y=294.
mapView is my UIImageView, just for clarification.
So my second try was to use the MKMapKit:
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(49.993615, 8.242493); MKMapPoint point = MKMapPointForCoordinate(coord); NSLog(@"x is %f and y is %f",point.x,point.y);
But this gives me some really strange values:
x = 140363776.241755 and y is 91045888.536491. So do you have an idea what i have to do to get this working ?
Thanks so much !
- Getting the coordinates from the location I touch the touchscreen
- What event is fired when MKMAPVIEW extent changes
- GPS location on iOS and Android
- How do I increase or decrease the size of a button in Swift?
- Calculating bearing between two CLLocationCoordinate2Ds
- How to get coordinates (CGRect) for the selected text in UIWebView?
Solutions Collect From Internet About “Convert longitude/latitude to x/y on iPhone”
To make this work you need to know 4 pieces of data:
- Latitude and longitude of the top left corner of the image.
- Latitude and longitude of the bottom right corner of the image.
- Width and height of the image (in points).
- Latitude and longitude of the data point.
With that info you can do the following:
// These should roughly box Germany - use the actual values appropriate to your image double minLat = 54.8; double minLong = 5.5; double maxLat = 47.2; double maxLong = 15.1; // Map image size (in points) CGSize mapSize = mapView.frame.size; // Determine the map scale (points per degree) double xScale = mapSize.width / (maxLong - minLong); double yScale = mapSize.height / (maxLat - minLat); // Latitude and longitude of city double spotLat = 49.993615; double spotLong = 8.242493; // position of map image for point CGFloat x = (spotLong - minLong) * xScale; CGFloat y = (spotLat - minLat) * yScale;
y are negative or greater than the image’s size, then the point is off of the map.
This simple solution assumes the map image uses the basic cylindrical projection (Mercator) where all lines of latitude and longitude are straight lines.
To convert an image point back to a coordinate, just reverse the calculation:
double pointLong = pointX / xScale + minLong; double pointLat = pointY / yScale + minLat;
pointY represent a point on the image in screen points. (0, 0) is the top left corner of the image.
- What is the purpose of the init! failable initializer?
- NSNotification vs. dispatch_get_main_queue
- iOS Frame by Frame video playback forward/backward
- iOS 8 – get current date as DD/MM/YYYY
- UIGestureRecognizer and UITableViewCell issue
- Swift: how to set up an audio session that gracefully mixes with others (aka Pod)
- CAGradientLayer not autoresizing
- The difference between max and fmax (Cross platform compiling)
- Customized back button appears on UIImagePickerController
- Font sizes in UIWebView does NOT match iOS font size
- Can't tap from one TextInput to another
- Apple Testfiight App Submission Can't Click Next
- Setting a different section for each month with fetchedresultcontroller
- iOS: Migrating existing Core Data-database into iCloud
- Instantiating split view controller by identifier in OS X storyboard does not load view hierarchy