2010-01-13 11 views
26

से उपयोगकर्ता स्थान प्राप्त करें क्या उपयोगकर्ताओं को वर्तमान स्थान को webservice में जाने के लिए MKMapView के अपने स्थान प्रबंधक का उपयोग करना संभव है?एक MKMapView

मैं mapView.showsUserLocation=YES; है और यह मेरा स्थान पर एक वैध नीले डॉट वापसी करता है, लेकिन सिम्युलेटर, अपनी क्यूपर्टिनो में - जो ठीक है, लेकिन जब मैं

mapView.userLocation.coordinate.latitude, 180 के लिए अपने समकक्ष को देखो, एक CLLocationManager जबकि सही एक, 37.3317 देता है।

मैं अपने तीन टैब के लिए एकाधिक स्थान प्रबंधक होने से बचना चाहता हूं, इसलिए नक्शा व्यू का उपयोग करना उपयोगी होगा।

धन्यवाद।

+0

हाय जोएक, आपका मतलब क्या है "यह मेरे स्थान पर एक मान्य नीला बिंदु नहीं लौटाता है"? मेरे जवाब पर एक नज़र डालें कि यह सहायक हो सकता है। – vfn

+0

अच्छा सवाल! +1, लेकिन चयनित उत्तर कभी काम नहीं करेगा! số 1! –

+0

जो उत्तर यहां स्वीकार किए जाते हैं वह गलत है, यह सही समाधान के लिए काम नहीं करेगा [http://stackoverflow.com/questions/1449486/iphone-current-user-location-coordinates-showing- के रूप में 0-0] (http: // stackoverflow।com/प्रश्न/1449486/iphone-current-user-location-coordinates-show-as-0-0) उम्मीद है कि यह –

उत्तर

40

आप MKMapView से उपयोगकर्ता स्थान मिल सकता है। आप बस इसके पुनर्प्राप्ति में एक संपत्ति खो रहे हैं। यह होना चाहिए:

mapView.userLocation.location.coordinate.latitude; 

उपयोगकर्ता स्थान केवल एक CLLocation स्थान विशेषता और एक बूल अद्यतन विशेषता संग्रह करता है। निर्देशांक प्राप्त करने के लिए आपको स्थान विशेषता पर जाना होगा।

-Drew

संपादित करें: जब तक नक्शा लोड हो जाए, और यह जांच बहुत जल्दी शून्य वापस आ जाएगी MKMapView के userLocation अपडेट नहीं होता। इससे बचने के लिए, मैं MKMapViewDelegate विधि -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation का उपयोग करने का सुझाव देता हूं।

+3

हाय जोएक, यह आपके लिए काम किया। मेरे मामले में यह 0.0 लौटाता है। – Aisha

+0

मेरे मामले में इसके 0.0000 लौटने पर भी :( – Piscean

+0

यदि आपको किसी स्थान के रूप में 0.000 मिल रहे हैं, तो कृपया उपरोक्त उत्तर में मेरा संपादन देखें। –

3

तो, एक अनूठा CLLocateManager उपयोग करने के लिए, आप के बजाय करने के लिए आप सभी नक्शे के लिए प्रतिनिधि होने के लिए एक वर्ग बना सकते हैं, इसलिए,:।

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

करते हैं जैसे:

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

जहां MySharedDelegate आपकी कक्षा है सभी CLLocation प्रबंधक प्रतिनिधि विधियों के साथ।

आपको केवल एक मान्य, userLocation के लिए समन्वय

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

के पहले बुला के बाद प्राप्त कर सकते हैं इस विधि है क्योंकि जीपीएस नए स्थान मिल गया है और कि नीला बिंदु वहाँ में ले जाया जाएगा कहा जाता है जब और उपयोगकर्ता स्थान पर नया समन्वय होगा।

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

आप विचार मिल गया है:

जब यह पाया जाता है अपने CLLocationManager प्रतिनिधि वर्तमान स्थान लॉग इन करने पर निम्न विधि का उपयोग?

चीयर्स,
VFN

+0

समझ में आता है, तो यह ठीक है, मेरे प्रत्येक टैब के लिए एक अलग स्थान प्रबंधक होने के लिए और केवल प्रतिनिधि को साझा करें फिर? या क्या मुझे ऐप प्रतिनिधि में एक स्थान प्रबंधक आवंटित करना चाहिए और उसका उपयोग करना चाहिए? धन्यवाद। – 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