Going from Google Maps to MapKit on iOS
I am currently using the following code:
UIApplication *app = [UIApp开发者_运维知识库lication sharedApplication];
[app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?saddr=Current%20Location&daddr=Chicago"]];
It works perfectly fine for opening up the directions in the google maps App. If I wanted to do this EXACT same thing, only within the App itself, how would I do it? I currently have a viewController set up that has an instance of MKMapView, but it shows the entire world. Everything is working fine but as soon as I try to read the Apple Documentation on Annotations my head starts to spin.
The annotations part is reasonably straightforward once you wrap your head around it, but the path-drawing part is a tremendous hassle. I do it in one of my apps and it took a lot of work to not just draw the lines and keep them scaled and such, but more importantly do it in a speedy and memory-efficient way if the user specifies a route that has literally hundreds of steps (cross-country avoiding highways, for example).
Unless the routing is the focus of your app, I wouldn't bother. If you still really want to I'll go back and review the code and provide some pointers. Just be forewarned that there's a surprisingly lot to it.
I am going to assume that you're doing a lookup for an address and displaying that in your app. You can use the Geocoder API to do a lookup with bounds (or without). I'm using this NSString format: NSString *geocoderURLFormat = @"http://maps.googleapis.com/maps/api/geocode/json?address=%@&bounds=%@&sensor=true"
.
The callback from this call will return a suggested viewport for the location -- southwest and northeast lat/long values. Using these, you can set the region of the MKMapView
object like this:
CLLocationCoordinate2D coord;
coord.latitude = location.latitude;
coord.longitude = location.longitude;
MKCoordinateSpan span;
span.latitudeDelta = location.swLatitude > location.neLatitude ? location.swLatitude - location.neLatitude : location.neLatitude - location.swLatitude;
span.longitudeDelta = location.swLongitude > location.neLongitude ? location.swLongitude - location.neLongitude : location.neLongitude - location.swLongitude;
MKCoordinateRegion region;
region.span = span;
region.center = coord;
[mapView setRegion:region animated:YES];
Note that I'm still a little fuzzy on the latitudeDelta
and longitudeDelta
values of MKCoordinateSpan
, hence the ugliness of the code. And the location
variable that I'm using is constructed from the results of the call to Geocoder.
Hope this helps!
精彩评论