27

MKMapView 独自のロケーション マネージャーを使用して、ユーザーの現在のロケーションを返して Web サービスに渡すことはできますか?

私は持ってmapView.showsUserLocation=YES;おり、これは私の場所で有効な青い点を返しますが、シミュレーターではそのクパチーノです-これは問題ありませんが、見ると

mapView.userLocation.coordinate.latitude、180 に等しいのに対し、CLLocationManager は正しい 37.3317 を返します。

3 つのタブに複数のロケーション マネージャーを配置することは避けたいので、独自の 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 に答える