开发者

iPhone Direction Arrow?

开发者 https://www.devze.com 2022-12-15 20:18 出处:网络
I need to create an iPhone simple view that, based on the location of the user in the world, points the person to a never-changing Long/Lat location.

I need to create an iPhone simple view that, based on the location of the user in the world, points the person to a never-changing Long/Lat location.

Is it possible to know this from the开发者_开发技巧 iPhone API?

Any examples?


This is possible on the current (3GS) iPhone, but not on earlier versions (which did not have a compass). The APIs that you'll need to use are in the CoreLocation.framework.

Search the app store for "mecca" and you'll turn up many, many applications that do exactly what you're asking about. You'll also find several applications that let you drop a pin anywhere on the map and have the app point you to it.


I've recently written code that does almost exactly what you describe.

Here's what you ned to do:

Calculate a heading from your current location to your target location. You should use "great circle" calculations, so they show a correct heading even when the destination is over the horizon. I found code (in Javascript) at this link to show me how to do this:

http://www.movable-type.co.uk/scripts/latlong.html

You want the section titled "bearing" That code uses javascript library routines like "math.sin(x)" You can pretty much just delete the "math." part, and the trig functions work as is.

That will give you your bearing in radians.

You then need to get your compass heading (if on a 3Gs phone), convert it to radians, and use the compass heading to correct for the orientation of the phone. If you're running on a 3G, you can skip the compass heading and show the bearing based on North being at the top of the phone, and let the user orient their phone towards North themselves.

Duncan C

0

精彩评论

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