Samrat Mazumdar
Samrat Mazumdar

Reputation: 2681

How to make Google Maps link with location data in iOS

I have the latitude & Longitude data of a location, how can I make it a Google Maps link, when the user clicks on share and choose option like email.

Here is the code that I use to get location data:

// Here is the .h file

@interface locate : UIViewController <CLLocationManagerDelegate,MKMapViewDelegate> 
{
    CGPoint gestureStartPoint;
    CLLocationManager *locationManager;
    CLLocation        *startingPoint;

    UILabel *latitudeLabel;
    UILabel *longitudeLabel;
    UILabel *altitudeLabel;
    MKMapView *mapView;
}

@property (assign) CGPoint gestureStartPoint;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *startingPoint;
@property (nonatomic, retain) IBOutlet UILabel *latitudeLabel;
@property (nonatomic, retain) IBOutlet UILabel *longitudeLabel;
@property (nonatomic, retain) IBOutlet UILabel *altitudeLabel;
@property (nonatomic, retain)  IBOutlet  MKMapView *mapView;
@end

// Here is the .m file

#import "locate.h"


@implementation locate
@synthesize gestureStartPoint,locationManager,startingPoint,latitudeLabel,longitudeLabel,altitudeLabel,mapView;


- (void)dealloc
{
    [locationManager release];
    [startingPoint release];
    [latitudeLabel release];
    [longitudeLabel release];
    [altitudeLabel release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

    if (startingPoint == nil)
        self.startingPoint = newLocation;

    NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g\u00B0",
                                newLocation.coordinate.latitude];
    latitudeLabel.text = latitudeString;
    [latitudeString release];

    NSString *longitudeString = [[NSString alloc] initWithFormat:@"%g\u00B0",
                                 newLocation.coordinate.longitude];
    longitudeLabel.text = longitudeString;
    [longitudeString release];

    NSString *altitudeString = [[NSString alloc] initWithFormat:@"%gm",
                                newLocation.altitude];
    altitudeLabel.text = altitudeString;
    [altitudeString release];

}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {

    NSString *errorType = (error.code == kCLErrorDenied) ?
    @"Access Denied" : @"Unknown Error";
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Error getting Location"
                          message:errorType
                          delegate:nil
                          cancelButtonTitle:@"Okay"
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}

- (void)viewDidLoad
{
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    mapView.delegate = self;
    mapView.mapType = MKMapTypeStandard;
    [super viewDidLoad];


}

- (void)viewDidUnload
{
    self.locationManager = nil;
    self.latitudeLabel = nil;
    self.longitudeLabel = nil;
    self.altitudeLabel = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

@end

Now please someone help me on how can I use the location data to create Google Maps link?

Upvotes: 2

Views: 2392

Answers (3)

oscar castellon
oscar castellon

Reputation: 3138

You need this code.

NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%1.6f,%1.6f",
                                 newLocation.coordinate.latitude,
                                 newLocation.coordinate.longitude];

linkMap.text = googleMapsURLString;

Upvotes: 2

Vassan
Vassan

Reputation: 56

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                       [txtf_mapsearch.text 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 {
    //Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;

return location;

Upvotes: 3

Dmorneault
Dmorneault

Reputation: 199

Try this

NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
                                 self.currentLocation.coordinate.latitude,
                                 self.currentLocation.coordinate.longitude,
                                 longitude,
                                 latitude];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];

where latitude and longitude are the point of interest.

Upvotes: 1

Related Questions