Launcing google maps app from my app iphone - iphone

I am trying to launch Google maps app from my app. In the maps app I was to set the destination and the user decides to use his current location or enter address. Can any one guide me how to proceed with this. I absolute have no idea about this.
Thanks

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://maps.google.com/maps?q=INSERT_YOUR_QUERY_HERE"]]

To load the current location you can use
mapView.showsUserLocation = YES;
To find the location you need to use google API as follows.
-(CLLocationCoordinate2D)findTheLocation{
NSString *url = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",[addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:NULL];
NSArray *addressItems = [locationString componentsSeparatedByString:#","];
NSLog(#"Address URL : %#", locationString);
double latitude;
double longitude;
if ([addressItems count] >=4 && [[addressItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[addressItems objectAtIndex:2] doubleValue];
longitude = [[addressItems objectAtIndex:3] doubleValue];
}
else {
NSLog(#"Address error.....");
}
CLLocationCoordinate2D coord;
coord.latitude = latitude;
coord.longitude = longitude;
return coord;
}
Above function will return the coordinates of the address. You can display this coordination on map.
I hope this will help you.

Related

How get latitude and longitude of whole address?

I am using google direction API in which when i enter only name of city then it give the value of latitude and longitude of source and destination of city address. While i am enter whole address like as B-1,ridhi sidhi complex, Chopra katla,Rani bazar, Bikaner then i don't give any latitude and longitude. How i get latitude and longitude of that address from Google direction API?
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",
[loactionTextField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]
encoding:NSUTF8StringEncoding
error:nil];
NSArray *array = [locationString componentsSeparatedByString:#","];
if(array == nil)
{
return;
}
else
{
if(array.count > 3)
{
lat = [[array objectAtIndex:2] doubleValue];
longt = [[array objectAtIndex:3] doubleValue];
}
}
I tried your address in this and it does return lat and long.

Objc_msgSend and app crash

When I trying to launch my app on simulator (3.1.3 , Xcode 3.1.4), it shows me objc_msgSend and application does not launch. This happens only when I alloc NSMUtable Array
This is my code part,
in viewDidLoad,
-(void)viewDidLoad{
locationArray = [[NSMutableArray alloc] initWithObjects:#"new delhi", #"faridabad",#"meerut"];
str1=[locationArray objectAtIndex:0];
str2=[locationArray objectAtIndex:1];
[super viewDidLoad];
}
Then I want to use locationArray objects in following method;
-(CLLocationCoordinate2D) addressLocation1{
double latitude = 0.0;
double longitude = 0.0;
NSString *str= [locationArray NSString *str= [locationArray objectAtIndex:0];
//NSString *str= #"new delhi"
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",
[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
problem occur only when alloc the locationArray, whats wrong with me, please help
thanks
locationArray = [[NSMutableArray alloc] initWithObjects:#"new delhi", #"faridabad",#"meerut"];
Try
locationArray = [[NSMutableArray alloc] initWithObjects:#"new delhi", #"faridabad",#"meerut", nil];
From The NSArray docs you see that for initWithObjects: it requires termination with nil. If you don't want to do that, and you know how many you have, you can use initWithObjects:count:

Speed up addAnnotations from Google Geocoding?

I have the following code in my app which adds a number of Annotations to a mapview via Google geocoding:
for (PlaceObject *info in mapLocations) {
NSString * addressOne = info.addressOne;
NSString * name = info.name;
NSString * address = [addressOne stringByAppendingString:#",London"];
NSError * error;
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString ] encoding:NSASCIIStringEncoding error:&error];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
} else {
//Error handling
}
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude;
coordinate.longitude = longitude;
MyLocation *annotation = [[[MyLocation alloc] initWithName:name address:address coordinate:coordinate] autorelease];
[mapViewLink addAnnotation:annotation];
}
This works, however it takes a long time for the view to appear as it seems to wait until all objects in the array have been looped through (there are about 80).
Is there any way to make the view load and then for the pins to be added as they are created ?
How about this:
for (PlaceObject *info in mapLocations) {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// GET ANNOTATION INFOS
NSString * addressOne = info.addressOne;
NSString * name = info.name;
NSString * address = [addressOne stringByAppendingString:#",London"];
NSError * error;
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString ] encoding:NSASCIIStringEncoding error:&error];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
} else {
//Error handling
}
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude;
coordinate.longitude = longitude;
MyLocation *annotation = [[[MyLocation alloc] initWithName:name address:address coordinate:coordinate] autorelease];
dispatch_sync(dispatch_get_main_queue(), ^{
// ADD ANNOTATION
[mapViewLink addAnnotation:annotation];
});
});
}
But please consider to double-check this code. There are definitily things you should do about keeping the threads safe and avoid EXC_BAD_ACCESS.
Where is this code executed in your app?
I'd recommend using NSURLConnection and adding the annotations after the data has been received and parsed. Look here for more info, if you're not familiar with NSURLConnection: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
Yea, you are making the classic error of doing long work (net loading from a URL) in the main (UI) thread. Move this work into a background thread and then have the main thread add the pins. This can be in several ways, by creating another explicit thread, GCD with blocks, or just performSelector in background thread. Easiest way is to probably performSelector in background and then when it has something to add to map, performSelector on main thread.

How to add annotation in Map view In Iphone

I want to place an annotation on the Map view from the address typed in a text field using the MapKit instead of longitude and latitude in iphone.Whether is it possible to do that?If yes,how. Please provide some sample code to do this. Thanx in advance
You have to get the lat/long via some manner (geocoding). Here's an example I found on an excellent MKMapView tutorial:
NSString * address = #ā€1600 Pennsylvania Ave, Washington DCā€; // address here
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#ā€200ā€³]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Error handling
}
From http://www.invasivecode.com/2009/07/adding-maps-to-your-iphone-app-mapkit/

How can I determine a CLLocation or MKPlacemark from an address?

I've seen API documentation and demo after demo on how to do a reverse geocode - to get an address, given a Lat/Long location. I need to do the reverse. I'm assuming that this is already solved problem since Apple's MapKit API avoids it entirely.
One way is to use the google webservice like so:
-(CLLocationCoordinate2D) addressLocation {
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",
[addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSLog(#"locationString = %#",locationString);
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
/*
NSString *nameString = [NSString stringWithFormat:#"http://ws.geonames.org/findNearestAddress?lat=%f&lng=%f",latitude,longitude];
NSString *placeName = [NSString stringWithContentsOfURL:[NSURL URLWithString:nameString]];
NSLog(#"placeName = %#",placeName);
*/
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}