如何在iOS中将Google地图与位置数据相关联

Sam*_*dar 2 maps xcode ios

我有一个位置的纬度和经度数据,当用户点击共享并选择电子邮件等选项时,如何将其设为Google地图链接.

这是我用来获取位置数据的代码:

// 这是.h文件

@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
Run Code Online (Sandbox Code Playgroud)

// 这是.m文件

#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
Run Code Online (Sandbox Code Playgroud)

现在请有人帮助我如何使用位置数据创建Google地图链接?

小智 5

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;
Run Code Online (Sandbox Code Playgroud)