2011-07-31 9 views
2

私は緯度が&です。場所の緯度データは、ユーザーが共有をクリックしてメールのようなオプションを選択するとどうすればGoogleマップのリンクにすることができますか? .mファイルGoogleマップをiOSの位置情報とリンクさせるには

の.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 

// ここ

// される:ここで

は、私は、位置データを取得するために使用したコードです
#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マップのリンクを作成するために位置情報を使用することができますか?

答えて

1

は、緯度と経度が注目点であるこの

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

を試してみてください。

+0

おかげで、私は書くことができない "のNSString * googleMapsURLString = [NSStringのstringWithFormat:のhttp:?//maps.google.com/maps Q = <私がここに書くべきか記入してください> ]; ' URLは現在の場所だけ必要です –

2

このコードが必要です。

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

linkMap.text = googleMapsURLString; 
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; 
関連する問題