2010-01-13 11 views
26

È possibile utilizzare il proprio gestore di posizione di MKMapView per restituire agli utenti la posizione corrente da passare in un servizio web?Ottieni posizione utente da un MKMapView

devo mapView.showsUserLocation=YES; e questo fa tornare un punto blu valida alla mia posizione, ma nel simulatore, il suo Cupertino - che va bene, ma quando guardo

mapView.userLocation.coordinate.latitude, la sua uguale a 180, mentre un CLLocationManager restituisce quello corretto, 37.3317.

Desidero evitare di avere più gestori di località per le mie tre schede, quindi utilizzare le immagini di mappe personali sarebbe utile.

Grazie.

+0

Ciao joec, cosa intendi con "questo non restituisce un punto blu valido nella mia posizione"? Dai un'occhiata alla mia risposta che può essere utile. – vfn

+0

Buona domanda! +1, ma la risposta selezionata non funzionerà mai! quindi -1! –

+0

Le risposte accettate qui sono errate, non funzionerà, per la soluzione giusta leggere le risposte di questa domanda [http://stackoverflow.com/questions/1449486/iphone-current-user-location-coordinates-showing- as-0-0] (http: // StackOverflow.it/questions/1449486/iphone-current-user-location-coordinates-shows-as-0-0) Spero che questo abbia senso –

risposta

40

È possibile ottenere la posizione utente dal MKMapView. Ti manca solo una proprietà nel tuo recupero di esso. Dovrebbe essere:

mapView.userLocation.location.coordinate.latitude; 

userLocation memorizza solo un attributo di posizione CLLocation e un attributo di aggiornamento BOOL. Devi andare all'attributo location per ottenere le coordinate.

-Drew

EDIT: userLocation del MKMapView non aggiorna fino a quando la carta ha terminato il caricamento, e verificando troppo presto tornerà zeri. Per evitare questo, suggerisco di utilizzare il metodo MKMapViewDelegate -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation.

+3

Ciao joec, ha fatto questo lavoro per te. Nel mio caso restituisce 0.0. – Aisha

+0

restituendo 0.0000 nel mio caso :( – Piscean

+0

Se stai ricevendo 0.000 come posizione, consulta la mia modifica nella risposta sopra. –

3

Quindi, per usare un CLLocateManager unico, è possibile creare una classe di essere il delegato per tutti voi le mappe, così, invece di fare:.

self.locationManager = [[CLLocationManager alloc] init]; 
    _locationManager.delegate = self;

fare qualcosa di simile:

self.locationManager = [[CLLocationManager alloc] init]; 
    _locationManager.delegate = mySharedDelegate;

Dove mySharedDelegate è la classe con tutti i metodi delegati di CLLocationManager.

Si può ottenere solo una valida coordinata per l'userLocation, dopo la prima chiamata di

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

Quando questo metodo viene chiamato è perché il GPS ha trovato nella nuova posizione e quindi il punto blu verrà spostato in là e la posizione utente avrà la nuova coordinata.

utilizzare il seguente metodo sul vostro delegato CLLocationManager per registrare la posizione corrente quando si trova:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"---------- locationManager didUpdateToLocation"); 
    location=newLocation.coordinate; 

    NSLog(@"Location after calibration, user location (%f, %f)", _mapView.userLocation.coordinate.latitude, _mapView.userLocation.coordinate.longitude); 
}

Avete avuto l'idea?

Cheers,
VFN

+0

Va bene quindi, avere un gestore di località separato per ciascuna delle mie schede e condividere solo il delegato poi? O dovrei allocare e avviare un gestore di località nel delegato dell'app e usarlo? Grazie. – joec

0
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
    { 
     NSLog(@"welcome into the map view annotation"); 

     // if it's the user location, just return nil. 
     if ([annotation isKindOfClass:[MyMapannotation class]]) 
     { 
      MyMapannotation *annotation12=(MyMapannotation *)annotation; 
      // try to dequeue an existing pin view first 
      static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
      MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc] 
              initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] ; 
      pinView.animatesDrop=YES; 
      pinView.canShowCallout=YES; 
      pinView.pinColor=MKPinAnnotationColorPurple; 
      pinView.tag=annotation12.tag; 

      UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
      rightButton.tag=annotation12.tag; 
      [rightButton addTarget:self 
          action:@selector(showDetails:) 
        forControlEvents:UIControlEventTouchUpInside]; 
      pinView.rightCalloutAccessoryView = rightButton; 
      UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"artpin"]]; 
      pinView.image = profileIconView.image; 
      return pinView; 
     } 
     else 
      return nil; 
    } 

    -(IBAction)showDetails:(id)sender 
    { 
     UIButton *btn=(UIButton *)sender; 

    } 


    -(void)Load_mapview 
    { 


     for (int i=0; i<[arr_nearby count]; i++) 
     { 
      NSNumber *latitude = [[[[arr_nearby objectAtIndex:i] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lat"]; 

      NSNumber *longitude = [[[[arr_nearby objectAtIndex:i] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lng"]; 

      NSString *title = [[arr_nearby objectAtIndex:i] valueForKey:@"name"]; 

      //Create coordinates from the latitude and longitude values 

      CLLocationCoordinate2D coord; 

      coord.latitude = latitude.doubleValue; 

      coord.longitude = longitude.doubleValue; 

      MyMapannotation *annotation = [[MyMapannotation alloc] initWithTitle:title AndCoordinate:coord andtag:i]; 
      [_map_nearby addAnnotation:annotation]; 


      // [annotations addObject:annotation]; 

     } 

     [self zoomToLocation]; 


    } 
    -(void)zoomToLocation 

    { 

     CLLocationCoordinate2D zoomLocation; 

     zoomLocation.latitude = [[[[[arr_nearby objectAtIndex:0] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lat"] floatValue]; 

     zoomLocation.longitude= [[[[[arr_nearby objectAtIndex:0] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lng"] floatValue]; 

     MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 7.5*5,7.5*5); 

     [_map_nearby setRegion:viewRegion animated:YES]; 

     [_map_nearby regionThatFits:viewRegion]; 

    } 

// 
// MyMapannotation.h 
// IOS_Googgle 
// 
// Created by Vivek Chauhan on 27/06/16. 
// Copyright (c) 2016 anand. All rights reserved. 
// 

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 


@interface MyMapannotation : NSObject <MKAnnotation> 

@property (nonatomic,copy) NSString *title; 
@property (nonatomic,assign) int tag; 


@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

-(id) initWithTitle:(NSString *) title AndCoordinate:(CLLocationCoordinate2D)coordinate andtag:(int)tagofbutton; 


@end 


// 
// MyMapannotation.m 
// IOS_Googgle 
// 
// Created by Vivek Chauhan on 27/06/16. 
// Copyright (c) 2016 anand. All rights reserved. 
// 

#import "MyMapannotation.h" 

@implementation MyMapannotation 

@synthesize coordinate=_coordinate; 

@synthesize title=_title; 
@synthesize tag=_tag; 


-(id) initWithTitle:(NSString *) title AndCoordinate:(CLLocationCoordinate2D)coordinate andtag:(int)tagofbutton 

{ 

    self = [super init]; 

    _title = title; 

    _coordinate = coordinate; 
    _tag=tagofbutton; 

    return self; 

} 
@end 
Problemi correlati