开发者

Help calculating X and Y from Latitude and Longitude in iPhone

I'm stuck into some kind of geolocation limbo using a custom map with iPhone. The problem is: I have an UIScrollView and an UIImageView with a image of a custom map for a fair, with all stand locations and so on. My biggest problem is that I intend to use geolocation in order to locate the person inside that map. The problem begins when I try to get the pixel location of a given latitude and longitude. I have several ground con开发者_开发知识库trol points as reference and even managed to calculate the an angle between my reference spot and the current location. The problem is that I can't correlate the distance in pixels with a distance calculated with the geolocation coordinates.

I am trying to relate them as follows:

Calculating the distance in pixels of a known point in my map. Calculating the distance using vector distance calculation for two reference points. Equaling the two above to find the actual distance from the reference.

But I am having no success at all...

Using the law of cosines, I can find the angle that will give my projections of X and Y but I can't find a scale to multiply correctly. I guess is because latitude and longitude are given in degrees and are non-linear, but I have such a small spot that I can aproximate it as linear spot.

I can't use an overlay image in MKMapKit because I have to use the map horizontally and in google maps, the same place is rotate several degrees to the left.

UPDATE:

Following this site: http://www.movable-type.co.uk/scripts/latlong.html, i could calculate the distance using the Haversine formula, but as orientation bellow, i found out that I was calculating the angle in a wrong way. I will have to find another way to calculate the angle.


I would do this by assuming flat earth approximation and use vector algebra to relate angles and distances in the lat,lon space to the x,y pixel space.

For example:

I am assuming you know the lat,lon for the bottom left and bottom right corners. Also assuming your fair map isn't near the poles and is fairly small in area.

Pick a coordinate system say bottom left corner with known lat,lon at 0,0 pixels. Called lat1,lon2 in following pseudo code

Compute vector 1 from the bottom right lat2,lon2 to the bottom left lat1,lon1

Using simple projection xl=lon, yl=lat, then vector 1 = (lon2 - lon1)i + (lat2-lat1)j

Compute vector 2 from the lat,lon position of person you want (latp,lonp) to put on the map to the bottom left point lat1,lon1

Use vector dot product to get the angle between vector 1 and 2.

Compute the distance in lat,lon space via equirectangular projection:

p1 = (lonp - lon1) cos ( 0.5*(latp+lat1) ) (convert lat/lon to radians)
p2 = (latp - lat1)
distance = R * sqrt( p1*p1 + p2*p2)
use R = 6371000 and your distance will be in meters

Now scale this distance to your map scale

At this point, you have polar coordinates of the point in pixel space

you now do a polar to rectangular conversion; x = r cos(angle), y = r sin(angle)

r is the scaled distance (i.e. distance in pixel space) and angle is the angle between vector 1 and 2 above

As a sanity check, you could compute the angle of the lat,lon vectors created from the top left to bottom left dotted with the bottom right to bottom left. If the angle isn't close to 90 degrees, there may be too much distortion for your purposes.


Not sure if I am understanding this correctly.. but over a small distance you should be able to just scale Lat/Long to y/x coordinates.. or you can go the other way.

What I would do is work out/look up the lat/long coordinates of the corners of your image and then scale points in between to a pixel location in that range. For example:

Let's say the top left of your image (101px wide) represents lat/lng (0,100) and the top right (0,100.1). Now if you want to place the point (0,100.05), you would simply place it in the middle - ie at pixel (x,y) (51,0).

I would also convert all lat/lng from degree/minute/second to decimal degrees - ie. 150deg 30min becomes 150.5.


I think you're looking for screen projections.

google.maps.Projection object specification

fromLatLngToPoint(latLng:LatLng, point?:Point)

Translates from the LatLng cylinder to the Point plane. This interface specifies a function which implements translation from given LatLng values to world coordinates on the map projection. The Maps API calls this method when it needs to plot locations on screen. Projection objects must implement this method.

http://code.google.com/apis/maps/documentation/javascript/reference.html#Projection

EDIT:

MKCoordinateForMapPoint Returns the latitude and longitude that corresponds to the specified map point.

CLLocationCoordinate2D MKCoordinateForMapPoint(
   MKMapPoint mapPoint
);

MKMapPointForCoordinate Returns the map point data structure that corresponds to the specified coordinate.

MKMapPoint MKMapPointForCoordinate(
   CLLocationCoordinate2D coordinate
);

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MapKitFunctionsReference/Reference/reference.html#//apple_ref/doc/uid/TP40008209

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜