27

是否可以使用 MKMapView 自己的位置管理器返回用户当前位置以传递给 web 服务?

我有mapView.showsUserLocation=YES;,这确实在我的位置返回了一个有效的蓝点,但在模拟器中,它的库比蒂诺 - 很好,但是当我看

mapView.userLocation.coordinate.latitude,它等于 180,而 CLLocationManager 返回正确的 37.3317。

我想避免为我的三个选项卡设置多个位置管理器,因此使用自己的 mapViews 会有所帮助。

谢谢。

4

3 回答 3

47

可以从 MKMapView 获取用户位置。您只是在检索它时丢失了一个属性。它应该是:

mapView.userLocation.location.coordinate.latitude;

userLocation 只存储一个 CLLocation 位置属性和一个 BOOL 更新属性。您必须转到位置属性才能获取坐标。

-画

编辑: MKMapView 的 userLocation 在地图加载完成之前不会更新,并且过早检查将返回零。为了避免这种情况,我建议使用 MKMapViewDelegate 方法 -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

于 2010-01-16T22:47:07.997 回答
3

因此,要使用唯一的 CLLocateManager,您可以创建一个类作为所有地图的委托。因此,不要这样做:

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

执行以下操作:

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

其中 mySharedDelegate 是您的类,其中包含所有 CLLocationManager 委托方法。

在第一次调用后,您只能获取 userLocation 的有效坐标

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

调用此方法是因为 GPS 已找到新位置,因此蓝点将移动到那里,并且 userLocation 将具有新坐标。

在您的 CLLocationManager 委托上使用以下方法在找到当前位置时记录它:

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

你有这个想法吗?

干杯,
VFN

于 2010-01-14T06:59:50.697 回答
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
于 2016-06-27T10:48:03.190 回答