Going from Google Maps to MapKit on iOS - iphone

I am currently using the following code:
UIApplication *app = [UIApplication 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!

Related

json vs xml annotations for mapkit

I have been working on a maps application (iphone) originally I had my annotations set up to pull XML from google using their Places API. I'm having 3 issues.
For my annotation info, I was going
off of an example from Zen
(http://www.zen-sign.com/finding-business-listings-and-displaying-with-mapkit-part-1/
) and he has it set up to do it by
keyword, which wasn't really
necessary for me ( but I used it
anyway just to get a feel for
getting the annotations) in the
parser header he has:
-(void) getBusinessListingsByKeyword:(NSString*)keyword atLat:(float)lat atLng:(float)lng;
and in the the viewdidload of his
view controller
[locationsMap findLocationsByKeyword:#"Apple" ];
I'm not sure how to move from the
keyword parse version used in zen to
something that just does it
automatically (in the parser object- without the viewdidload in a different view controller if possible).
Any advice on what to read/watch or
sample code much appreciated
For places information
Google isn't the only kid on the
block and XML I hear comes in second
to JSON. So I wanted to know what
the best practice was for map
annotations made from business
information: JSON or XML?
The other issue I was having was
only getting 10 annotations (I want
to get 50 or more). So on top of
your advice on using XML or JSON.
How to I increase the amount of
annotations I'm getting.
Sorry for making this 3 parts but again any tutorials (text of video) would be very helpful. (So far I've watched hegarty from Stanford, Larson from MATC)
First
Don't know what you mean by automaticaly. But if you want to launch the map on users current location here you have two methods you can use:
-(IBAction)goToCurrentLocation{
CLLocation *location = [[CLLocation alloc]
initWithLatitude:myMap.userLocation.coordinate.latitude
longitude:myMap.userLocation.coordinate.longitude];
[self setCurrentLocation:location];
}
- (void)setCurrentLocation:(CLLocation *)location {
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = location.coordinate;
region.span.longitudeDelta = 0.15f;
region.span.latitudeDelta = 0.15f;
[self.myMap setRegion:region animated:YES];
}
Second. I don't know what are the best practicies but I used json with this jeson parser for my app ijustmadelove
Three. There is no problem in getting more then 10 annotations on the map. You have to have an error or a limitation in your code.

How to effectively draw Polyline using Google API?

i'm drawing polyline on MapView using "google direction API" and getting success in it.But the thing is that it draws straight line between two locations(with their longitude and latitude).I don't know how to draw line effectively like shown by google maps,when we click "get directions" between two locations.
I suggest taking a look at an SDK like CloudMade, this makes it very easy to implement custom maps and route's, you'll be able to draw a route between two locations within about 10 minutes of installing the SDK. Here is a quick example to get you started if you go down this route, excuse the pun.
CLLocationCoordinate2D initLocation;
initLocation.longitude = -0.127523;
initLocation.latitude = 51.51383;
CLLocationCoordinate2D destination;
destination.longitude = -0.125;
destination.latitude = 51;
TokenManager* tokenManager = [[TokenManager alloc] initWithApikey:#"YOUR API KEY"];
CMRoutingManager *routeManager = [[CMRoutingManager alloc] initWithMapView:mapView tokenManager:tokenManager];
[routeManager findRouteFrom:initLocation to:destination onVehicle:CMVehicleWalking];
You can find more information here in the documentation. Another alternative would be to use route-me which would allow you to choose from the following map sources:
OpenStreetMap, Microsoft VirtualEarth, CloudMade, OpenAerialMap, OpenCycleMap, SpatialCloud, and two offline, database-backed formats (DBMap and MBTiles)
Although personally i do not have any experience with route-me, so can't point you in the right direction to get started.
If you are adement on sticking with MKMapView, this blog post will get your started in the right direction.

MKErrorDomain error 4 iPhone

I keep getting this randomly when I run my gps app I'm building. It doesn't happen everytime, and the coordinates passed in are always valid (i nslog them). Is there documentation for these somewhere?
EDIT:
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(locManager.location.coordinate.latitude, locManager.location.coordinate.longitude);
geocoder1 = [[MKReverseGeocoder alloc] initWithCoordinate:coord];
geocoder1.delegate = self;
[geocoder1 start];
and then about half the time it returns an error. I tried releasing and re-assigning the geocoder if there was an error, but that didn't help. Only thing that did was restarting the app.
In "MKTypes.h" in the MapKit framework, the following is defined:
Error constants for the Map Kit framework.
enum MKErrorCode {
MKErrorUnknown = 1,
MKErrorServerFailure,
MKErrorLoadingThrottled,
MKErrorPlacemarkNotFound,
};
...
MKErrorPlacemarkNotFound
The specified placemark could not be found.
This sounds like you are referencing some unknown placemark in your code? Or it could be that Google doesn't have a name for the position you are passing - however valid the coordinates may be.
I've met and solved this issue recently. In my case, when Apple Map cannot find any result for a query, it sometimes will just throw this this "MKErrorDomain = 4" error. So I ended up just treat this as "result not found".
It was painstaking to find this out, MapKit needs a better Error handling system.
I've been hitting this error repeatedly, and was unable to figure out how to make it stop; but I finally found an end-run around the whole issue that works quite well, and only takes a little more work: Don't use Apple's MKReverseGeocoder at all -- instead, directly call Google's reverse-geocoding API (this is apparently the same service that MKReverseGeocoder does behind the scenes). You can get back either JSON or XML (your preference), which you will then have to parse, but that isn't too hard.
For example, since my app is using ASIHTTPRequest, this is what it looks like (although this would also be easy to do with do with Apple's native APIs such as NSURLConnection):
#pragma mark -
#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// Be careful: My code is passing sensor=true, because I got the lat/long
// from the iPhone's location services, but if you are passing in a lat/long
// that was obtained by some other means, you must pass sensor=false.
NSString* urlStr = [NSString stringWithFormat:
#"http://maps.googleapis.com/maps/api/geocode/xml?latlng=%f,%f&sensor=true",
newLocation.coordinate.latitude, newLocation.coordinate.longitude];
NSURL* url = [NSURL URLWithString:urlStr];
self.reverseGeocoderRequest = [ASIHTTPRequest requestWithURL:url];
self.reverseGeocoderRequest.delegate = self;
[self.reverseGeocoderRequest startAsynchronous];
}
By the way, Google's API has rules, just like Apple's does. Make sure you read the docs, especially regarding quotas.
I'm running into the same thing (the exact same code randomly fails sometimes) and I think I've found the answer. From Apple's developer docs: "Each Map Kit application has a limited amount of reverse geocoding capacity, so it is to your advantage to use reverse geocode requests sparingly."
So my theory is, we're getting rate-limited... since no other variables are changing (i.e. my code isn't changing, I'm running it on the simulator so the location of the device isn't changing, etc.) I think this must be the only remaining reason.
I just got done with a lot of research on this problem and it seems to be outside of our hands. I checked the developer forums as well as all around Stack and elsewhere and no one has a solution other than using a different service. There is a pretty good thread at https://devforums.apple.com/message/154126 on the subject.
Some people find the error after a certain time, I just find it to be out for a while and then comes back. I looked at the "Current Address" sample code and I couldn't see how I might have messed up. I ran the sample code and sure enough, it was NSLogging errors instead of returning a location.
This link has some code using Google's reverse geocoder: http://www.iphonedevsdk.com/forum/iphone-sdk-development/31883-pbrequestererrordomain-errors-reverse-geocoding.html#post155793
Actually, I am running into this problem as well. Code is extremely compact
//1. ask map for current coords
CLLocationCoordinate2D userLocation;
userLocation.latitude = [[_theMapView.userLocation location] coordinate].latitude;
userLocation.longitude = [[_theMapView.userLocation location] coordinate].longitude;
NSLog(#"%f, %f",userLocation.latitude,userLocation.longitude);
//2. reverse geocode coords
MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc]
initWithCoordinate:userLocation];
[reverseGeocoder setDelegate:self];
[reverseGeocoder start];
and later simply NSLog the error in the fail delegate method. It works the first time or two, then stops working

How can I group MKAnnotations automatically regarding zoom level?

if the user zooms out on a MKMapView, i want MKAnnotations which are near to each other automatically grouped into one "group" annotation.
if the user zooms back in, the "group" annotation should be split again to the unique/original annotations.
apple does this already in the iOS 4 Photos.app
is there a common, "predefined" way to do this?
Its normal working with more than 1500 annotations on the map:
-(void)mapView:(MKMapView *)mapView_ regionDidChangeAnimated:(BOOL)animated
{
NSMutableSet * coordSet = [[NSMutableSet alloc] init];
for(id<MKAnnotation> an in mapView_.annotations)
{
if([an isKindOfClass:[MKUserLocation class]])
continue;
CGPoint point = [mapView_ convertCoordinate:an.coordinate toPointToView:nil];
CGPoint roundedPoint;
roundedPoint.x = roundf(point.x/10)*10;
roundedPoint.y = roundf(point.y/10)*10;
NSValue * value = [NSValue valueWithCGPoint:roundedPoint];
MKAnnotationView * av = [mapView_ viewForAnnotation:an];
if([coordSet containsObject:value])
{
av.hidden = YES;
}
else
{
[coordSet addObject:value];
av.hidden = NO;
}
}
[coordSet release];
}
That's a brilliant idea. I'm working on a similar app, I hope you don't mind if I als implement the concept :).
To answer your question to the best of my own ability, no, I don't think there is a predefined way to do this.
The best way I can think of to do it (after looking at the iOS4 photos app), is to make use of the mapView:regionDidChangeAnimated: delegate method. Any time the user scrolls/zooms, this method will be called.
Inside that method, you could have some quick geometry math to determine whether your points are "close enough" to consider merging. Once they're "merged", you'd remove one or both annotations, and put another annotation back in the same place that is a reference to both (you could make an AnnotationCluster class very easily that could conform to MKAnnotation but also hold an NSArray of annotations, and also contain methods for "breaking out" or "absorbing" other annotations and/or AnnotationCluster instances, etc).
When I say "quick geometry math", I mean the distance of the two points relative to the span of the map, and taking their relative distance as a percentage of the span of the whole map.
Where that would get tricky is if you had hundreds of annotations, as I can't off-hand think of a good way to implement that w/o a double loop.
What do you reckon?
This project does something interesting. Though, have a look at reported issues before changing too many things in your code. Because it could be not good enough for your needs yet.
I personnaly ended up implementing this

MKMapView memory usage grows out of control with setRegion: calls

I have a single MKMapView instance that I have programmatically added to a UIView. As part of the UI, the user can cycle through a list of addresses and the map view is updated to show the correct map for each address as the user goes through them. I create the map view once, and simply change what it displays with setRegion:animated.
The problem is that each time the map is changed to show a new address, the memory usage of my program increases by 200K-500K (as reported by Memory Monitor in Instruments). According to Object Allocations, it appears that a lot of 1.0K Mallocs are happening each time, and the Extended Detail pane for these 1.0K allocations shows that the Responsible Caller is convert_image_data and the Extended Detail pane shows that this is the result of [MKMapTileView drawLayer:inContext:]. So, seems likely to me that the memory usage is due to MKMapView not freeing memory it uses to redraw the map each time. In fact, when I don't display the map at all (by not even adding it as a subview of my main UIView) but still cycle through the addresses (which changes various UILabels and other displayed info) the memory usage for the app does NOT increase. If I add the map view but never update it with setRegion:, the memory also does NOT increase when changing to a new address.
One more bit of info: if I go to a new address (and therefore ask the map to display the new address) the memory jumps as described above. However, if I go back to an address that was already displayed, the memory does not jump when the map redraws with the old address. Also, this happens on iPad (real device) with 3.2 and on iPhone (again, real device) with 3.1.2.
Here's how I initialize the MKMapView (I only do this once):
CGRect mapFrame;
mapFrame.origin.y = 460; // yes, magic numbers. just for testing.
mapFrame.origin.x = 0;
mapFrame.size.height = 500;
mapFrame.size.width = 768;
mapView = [[MKMapView alloc] initWithFrame:mapFrame];
mapView.delegate = self;
[self.view insertSubview:mapView atIndex:0];
And in response to the user selecting an address, I set the map like so:
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=kStreetMapSpan; // 0.003
span.longitudeDelta=kStreetMapSpan; // 0.003
region.center = address.coords; // coords is CLLocationCoordinate2D
region.span = span;
mapView.region.span = span;
[mapView setRegion:region animated:NO];
Any thoughts? I've scoured the net but haven't seen mention of this problem, and I've reached the limits of my Instruments knowledge. Thanks for any ideas.
What happens when you cycle back through to the original address? Does it still increase?
My thought is that it is likely caching map data, so it will naturally increase as it keeps other locations in memory.