【问题标题】:Default location in map-kit in iosios中map-kit中的默认位置
【发布时间】:2016-05-05 06:34:08
【问题描述】:

我是 IOS 新手,我需要用我的纬度和经度显示默认位置(12.940358、80.208647)。 带注释针。

.h 文件:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface Map_view : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate>{
    CLLocationManager *locationmgr;
    CLGeocoder *geocode;
    CLPlacemark *placemark;

}
@property(nonatomic,retain)IBOutlet MKMapView *map11;

.m 文件:

@synthesize map11;

我不知道在哪里放我的经纬度以及如何使用请帮帮我。

【问题讨论】:

    标签: ios mapkit mapkitannotation


    【解决方案1】:

    如果您想使用带有名称的注释来显示您的经纬度位置。

    使用此代码。

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        mapVW.showsUserLocation = YES;
        CLLocation *loc = [[CLLocation alloc]initWithLatitude:12.940358 longitude:80.208647];
    
        [mapVW setCenterCoordinate:loc.coordinate animated:YES];
    
        CLGeocoder *ceo = [[CLGeocoder alloc]init];
        [ceo reverseGeocodeLocation:loc
                  completionHandler:^(NSArray *placemarks, NSError *error) {
                      CLPlacemark *placemark = [placemarks objectAtIndex:0];
    
                      //String to hold address
                     NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                     NSString *cityName = placemark.locality;
    
                      MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
                      annotationPoint.coordinate = CLLocationCoordinate2DMake(12.940358, 80.208647);
                      annotationPoint.title = locatedAt;
    
                      MKCoordinateRegion region;
    
                      MKCoordinateSpan span1;
                      span1.latitudeDelta =  0.08;
                      span1.longitudeDelta = 0.08;
                      region.span = span1;
                      region.center = annotationPoint.coordinate;
                      [mapVW setRegion:region animated:TRUE];
                      [mapVW regionThatFits:region];
    
                      [mapVW addAnnotation:annotationPoint];
                      [mapVW selectAnnotation:annotationPoint animated:NO];
                  }
         ];
    
    }
    

    【讨论】:

    • 如果两个纬度和经度意味着@Kuldeep
    【解决方案2】:

    要以用户位置为中心,可以使用以下代码:

    [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
    

    对于特殊位置的缩放,您应该研究区域 (MKCoordinateRegion) 是如何计算和工作的,计算该区域的纬度和经度值并使用调用显示它:

    [mapView setRegion:myRegion animated:YES];
    

    【讨论】:

      【解决方案3】:

      试试下面的代码:

      CLLocationCoordinate2D zoomLocation;
      zoomLocation.latitude = 39.281516;
      zoomLocation.longitude= -76.580806;
      
      // 2
      MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
      
      // 3
      [_mapView setRegion:viewRegion animated:YES];
      

      此代码将在您的地图中设置您的区域..然后按照步骤在您的地图中添加注释..

      // Add an annotation
      MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
      point.coordinate = userLocation.coordinate; // Change coordinate as yours
      point.title = @"Where am I?";
      point.subtitle = @"I'm here!!!";
      
      [self.mapView addAnnotation:point];
      

      希望对你有帮助..

      【讨论】:

        【解决方案4】:

        使用此代码会对您有所帮助:

        - (void)location
        {
            _locationManger =[[CLLocationManager alloc]init];
            _locationManger.distanceFilter=kCLDistanceFilterNone;
            _locationManger.desiredAccuracy=kCLLocationAccuracyHundredMeters;
            [_locationManger startUpdatingLocation];
        
            [map11 setMapType:MKMapTypeStandard];
            [map11 setZoomEnabled:YES];
            [map11 setScrollEnabled:YES];
        
            MKCoordinateRegion region={ {0.0,0.0 },{0.0,0.0}};
        
            region.center.latitude = 12.940358 ;
            region.center.longitude = 80.208647;
            region.span.longitudeDelta = 20.20f;
            region.span.latitudeDelta = 20.20f;
        
            [map11 setRegion:region animated:YES];
            [map11 setDelegate:sender];
        
        }
        

        它将显示您想要的位置

        【讨论】:

        • 上面的代码显示错误使用未声明的标识符“发件人”。如果我评论那行显示通常的地图页面@Abhinandan Pratap
        • 注释该行并在 ViewDidLoad 中将其称为 [self location];
        【解决方案5】:

        在您的 viewdidload 中添加此代码

        //self.locationManager replace it with your locationmgr 
             self.locationManager = [[CLLocationManager alloc] init];
                self.locationManager.delegate = self;
            #ifdef __IPHONE_8_0
                if(IS_OS_8_OR_LATER) {
                    // Use one or the other, not both. Depending on what you put in info.plist
                    [self.locationManager requestWhenInUseAuthorization];
                    [self.locationManager requestAlwaysAuthorization];
                }
            #endif
                [self.locationManager startUpdatingLocation];
        
                mapView.showsUserLocation = YES;
                [mapView setMapType:MKMapTypeStandard];
                [mapView setZoomEnabled:YES];
                [mapView setScrollEnabled:YES];
        

        在 viewdidappear 添加下面的代码

        -(void)viewDidAppear:(BOOL)animated {
            [super viewDidAppear:YES];
        
            self.locationManager.distanceFilter = kCLDistanceFilterNone;
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            [self.locationManager startUpdatingLocation];
            NSLog(@"%@", [self deviceLocation]);
        
        
            CLLocationCoordinate2D userLocation = CLLocationCoordinate2DMake( self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude);
            MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
            [annotation setCoordinate:userLocation];
            [annotation setTitle:@"Your Location"];
            [self.mapView addAnnotation:annotation];
        
            //always show the name of annotation
            [self.mapView selectAnnotation:annotation animated:YES];
        
            //location 1
            CLLocationCoordinate2D loc1Coord = CLLocationCoordinate2DMake(18.998250, 72.848734);//add your latitude and longitude here
            MKPointAnnotation *annotation1 = [[MKPointAnnotation alloc] init];
            [annotation1 setCoordinate:loc1Coord];
            [annotation1 setTitle:@"title"];
            [annotation1 setSubtitle:@"subtitle"];
            [self.mapView addAnnotation:annotation1];
        
        }
        

        添加此方法

        - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
        {
            if ([annotation isKindOfClass:[MKUserLocation class]])
                return nil;
        
            MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"];
            annotationView.canShowCallout = YES;
            annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        
            return annotationView;
        }
        

        就是这样,你可以在地图上看到你的经纬度。让我知道它是否有效?

        【讨论】:

        • 是的,它可以正常工作,但不能放大,并且在 viewdidload 部分中的 if-else 中有一些错误@Madhu
        • 你能发布你的 viewdidload 代码并告诉我你哪里出错了。
        【解决方案6】:

        首先你必须设置 DELEGATE 并在您的 .h 文件中导入以下内容

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

        然后创建一个属性

        @property(retain, nonatomic)  CLLocationManager *locationManager;
        

        在 ViewDidLoad 中

        mapview.delegate = self;
        

        然后你可以像下面那样做

        - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
        {
            MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
            [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
        
            // Add an annotation
            MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
                point.coordinate = userLocation.coordinate;
            point.title = @"Where am I?";
            point.subtitle = @"I'm here!!!";
        
            [self.mapView addAnnotation:point];
        }
        
        - (void)didReceiveMemoryWarning {
            [super didReceiveMemoryWarning];
            // Dispose of any resources that can be recreated.
        }
        
        #pragma mark - to get User Current Location.
        - (void)getUserLocation{
            self.locationManager = [[CLLocationManager alloc] init];
        
        
            self.locationManager.delegate = self;
        
        
        #ifdef __IPHONE_8_0
            if([self getDeviceOSVersion].integerValue >= 8) {
                // Use one or the other, not both. Depending on what you put in info.plist
                [self.locationManager requestWhenInUseAuthorization];
                [self.locationManager requestAlwaysAuthorization];
            }
        #endif
            [self.locationManager startUpdatingLocation];
        }
        -(NSString *)getDeviceOSVersion{
            return [NSString stringWithFormat:@"%@",[UIDevice currentDevice].systemVersion];
        }
        
        #pragma mark - CLLocationManagerDelegate
        -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
            UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:@"ERROR" message:@"There was an error retrieving your location"preferredStyle:UIAlertControllerStyleAlert ];
            UIAlertAction* ok = [UIAlertAction
                                 actionWithTitle:@"OK"
                                 style:UIAlertActionStyleDefault
                                 handler:^(UIAlertAction * action)
                                 {
                                     [errorAlert dismissViewControllerAnimated:YES completion:nil];
        
                                 }];
            [errorAlert addAction:ok];
            [self presentViewController:errorAlert animated:YES completion:nil];
            NSLog(@"Error: %@",error.description);
        }
        
        -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
            CLLocation *currentLocation = [locations lastObject];
        //currentLocation.coordinate.longitude =  currentLocation.coordinate.longitude ;
           // latitude =  currentLocation.coordinate.latitude;
        
              [self.locationManager stopUpdatingLocation];
        
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-15
          • 1970-01-01
          • 2021-10-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-12
          • 2012-08-26
          相关资源
          最近更新 更多