简体   繁体   English

从MKMapView获取用户位置

[英]Obtain user location from a MKMapView

Is it possible to use the MKMapView's own location manager to return the users current location to pass into a webservice? 是否可以使用MKMapView自己的位置管理器返回用户当前位置以传递到Web服务?

I have mapView.showsUserLocation=YES; 我有mapView.showsUserLocation=YES; and this does return a valid blue dot at my location, but in the simulator, its Cupertino - which is fine, but when i look at 这确实在我的位置返回一个有效的蓝点,但在模拟器中,它的库比蒂诺 - 这很好,但当我看到

mapView.userLocation.coordinate.latitude , its equal to 180, whereas a CLLocationManager returns the correct one, 37.3317. mapView.userLocation.coordinate.latitude ,它等于180,而CLLocationManager返回正确的,37.3317。

I want to avoid having multiple location managers for my three tabs, so using the mapViews own would be helpful. 我想避免为我的三个选项卡设置多个位置管理器,因此使用mapViews会有所帮助。

Thanks. 谢谢。

You can get the user location from the MKMapView. 可以从MKMapView获取用户位置。 You are just missing a property in your retrieval of it. 您只是在检索它时遗漏了一个属性。 It should be: 它应该是:

mapView.userLocation.location.coordinate.latitude;

userLocation only stores a CLLocation location attribute and a BOOL updating attribute. userLocation仅存储CLLocation位置属性和BOOL更新属性。 You must go to the location attribute to get coordinates. 您必须转到location属性才能获取坐标。

-Drew -DREW

EDIT: The MKMapView's userLocation does not update until the map has finished loading, and checking too early will return zeros. 编辑:MKMapView的userLocation在地图加载完成之前不会更新,过早检查将返回零。 To avoid this, I suggest using the MKMapViewDelegate method -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation . 为避免这种情况,我建议使用MKMapViewDelegate方法-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

So, to use a unique CLLocateManager, you can create a class to be the delegate for all you maps., so, instead of doing: 因此,要使用唯一的CLLocateManager,您可以创建一个类作为所有映射的委托。所以,而不是:

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

Do something like: 做类似的事情:

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

Where mySharedDelegate is your class with all the CLLocationManager delegate methods. 其中mySharedDelegate是包含所有CLLocationManager委托方法的类。

You can only get a valid coordinate for the userLocation, after the first calling of 在第一次调用之后,您只能获得userLocation的有效坐标

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

When this method is called it is because the GPS has found the new location and so the blue dot will be moved to there and the userLocation will have the new coordinate. 当调用此方法时,这是因为GPS找到了新位置,因此蓝点将移动到那里,userLocation将具有新坐标。

Use the following method on your CLLocationManager delegate to log the current location when it is found: 在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);
}

Have you got the idea? 你有这个主意吗?

Cheers, 干杯,
VFN VFN

    - (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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM