Как сделать так, чтобы Карты Google связывались с данными о местоположении в iOS - PullRequest
2 голосов
/ 31 июля 2011

У меня есть данные о широте и долготе местоположения, как я могу сделать это ссылкой на Карты 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

// Вот файл .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

Теперь, пожалуйста, кто-нибудь, помогите мне, как я могу использовать данные о местоположении для создания ссылки на Google Карты?

Ответы [ 3 ]

5 голосов
/ 01 мая 2012
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;
2 голосов
/ 01 мая 2012

Вам нужен этот код.

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

linkMap.text = googleMapsURLString;
1 голос
/ 31 июля 2011

Попробуйте это

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]];

, где широтой и долготой являются точки интереса.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...