2010-01-13 12 views
26

Kullanıcıların mevcut bir web servisine geçmesi için MKMapView'ın kendi konum yöneticisini kullanmak mümkün müdür?Kullanıcı konum bilgisini bir MKMapView'dan alın

Ben mapView.showsUserLocation=YES; var ve bu benim konumda geçerli Mavi noktaya dönmek yok, ama simülatörü, onun Cupertino - iyi, ama ben bir CLLocationManager oysa

mapView.userLocation.coordinate.latitude, 180 onun eşit baktığınızda doğru olanı döndürür, 37.3317.

Üç sekmemde birden çok konum yöneticisine sahip olmaktan kaçınmak istiyorum, bu yüzden mapViews özelliğini kullanmak yararlı olacaktır.

Teşekkürler. .

+0

Merhaba joec, "Konumumda geçerli bir mavi nokta döndürmüyor" ile ne demek istiyorsunuz? Cevabımın faydalı olabileceğine bir bakın. – vfn

+0

İyi soru! +1, ancak seçilen cevap asla çalışmaz! số 1! –

+0

Burada kabul edilen cevaplar yanlış, işe yaramıyor, doğru çözüm için bu sorunun cevabını okuyun [http://stackoverflow.com/questions/1449486/iphone-current-user-location-coordinates-showing- as-0-0] (http: // stackoverflow.com/questions/1449486/iphone-geçerli-kullanıcı-konum-koordinatları-gibi-0-0) Umarım bu mantıklı olur –

cevap

40

Sen MKMapView gelen kullanıcı yerini alabilir. Geri aldığın bir mülkü kaçırıyorsun. Olmalıdır:

mapView.userLocation.location.coordinate.latitude; 

userLocation sadece CLLocation yeri özelliğine ve BOOL güncelleme özelliğini saklar. Koordinatları almak için konum özelliğine gitmelisiniz.

-Drew

DÜZENLEME: Harita yüklenmesi tamamlandıktan ve çok erken kontrol sıfır dönecektir kadar MKMapView en userLocation güncellemiyor. Bunu önlemek için MKMapViewDelegate yöntemini -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation kullanmanızı öneririz.

+3

Merhaba joec, bu iş sizin için yaptı. Benim durumumda 0,0 döndürür. – Aisha

+0

geri dönen 0,0000 benim durumumda da :( – Piscean

+0

Eğer konum olarak 0.000 alıyorsanız, lütfen yukarıdaki cevaba benim düzenlememe bakın. –

3

Yani, benzersiz CLLocateManager kullanmak, hepinizin haritaları için temsilci olmak için bir sınıf oluşturmak, böylece, yerine yapmanın edebilirsiniz:

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

yapın gibi bir şey:

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

mySharedDelegate, tüm CLLocationManager temsilci yöntemleriyle sınıfınızdır.

bu yöntem GPS yeni konumu bulmuş ve böylece mavi nokta var taşınacak çünkü öyle çağrıldığında sadece geçerli bir

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

ilk çağrı sonra, userLocation için koordinat alabilirsiniz ve userLocation yeni koordinatı alacak.

- (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); 
}

fikir var mı: o bulunduğunda

mevcut konumunu oturum açmak için CLLocationManager temsilci üzerinde şu yöntemi kullanın?

Alkış,
VFN

+0

Tamam, sekmelerimin her biri için ayrı bir konum yöneticisine sahip olmak ve sadece delege paylaşmak sonra? Yoksa uygulama delegesine bir konum yöneticisi tahsis etmeli ve onu kullanmalı mıyım? Teşekkürler. – 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 
İlgili konular