【问题标题】:User current Location on a mapkit in xcodexcode中mapkit上的用户当前位置
【发布时间】:2012-11-20 14:30:05
【问题描述】:

我需要一个很好的教程,在该教程中,用户的当前位置(纬度、经度、城市、州、国家)会随着位置的变化而不时更新,并随着蓝色图标的缩放显示在地图工具包上。

我通过在 view.xib 上放置一个 MKMapView 来做到这一点,它仅第一次显示用户的当前位置(模拟器上的默认设置:SanFransisco)。但是当我下次运行该应用程序时,它没有显示任何蓝点缩放。我应该写任何代码吗?直到现在我还没有写任何代码。刚刚在 xib 中放置了一个带有 Show UserLocation 的 mapkit。如何获得蓝点?

我还需要从用户位置找到附近的医生,并在同一张地图中显示红色标记。

通过谷歌搜索但很困惑。请在这方面向我推荐一些好的教程。

编辑:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mapView.delegate = self;
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.mapView setShowsUserLocation:YES];
    [locationManager startUpdatingLocation];
    [self queryGooglePlaces:@"doctor"];
}

-(void) queryGooglePlaces: (NSString *) googleType {

    // https://developers.google.com/maps/documentation/places/#Authentication
    NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=300&types=doctor&sensor=true&key=%@",  coord.latitude, coord.longitude,kGOOGLE_API_KEY];


    NSURL *googleRequestURL=[NSURL URLWithString:url];

        dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
}

//这里我将数组数据设为空,因为纬度和经度被传递为 0.000 ..

如何在 viewDidLoad 上同时获取它们?

【问题讨论】:

    标签: iphone ios mapkit


    【解决方案1】:

    如果您想找到附近的医生地址,您需要使用“GOOGLE PLACE API”

    //对于用户当前位置

    CLLocationCoordinate2D location = [[[mapview userLocation] location] 坐标];
    NSLog(@"从地图中找到的位置:%f %f",location.latitude,location.longitude);

    Check this link its good example of google place api

    【讨论】:

    • 如果你想使用州名和城市和国家名,你只能得到当前城市的纬度和经度,然后你想使用google place api
    • 我可以 c 他们。但在 [locationManager startUpdatingLocation] 之后,我无法在 viewDidLoad bcoz 上看到用户位置和附近的医生;它只是去 [self queryGooglePlaces:@"doctor"];不调用 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 事件
    【解决方案2】:

    添加ccl位置管理器的这个Delegate方法

    .h 文件

    double currentLatitude;
    double currentLongitude;
    
      - (void)locationManager:(CLLocationManager *)manager
            didUpdateToLocation:(CLLocation *)newLocation
                   fromLocation:(CLLocation *)oldLocation
        {
    
            currentLatitude = newLocation.coordinate.latitude;
    
            currentLongitude = newLocation.coordinate.longitude;
    
            [self  queryGooglePlaces:somestring];
        }
    
    -(void) queryGooglePlaces: (NSString *) googleType {
    
        // https://developers.google.com/maps/documentation/places/#Authentication
        NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=300&types=doctor&sensor=true&key=%@",  currentLatitude , currentLongitude,kGOOGLE_API_KEY];
    
    
        NSURL *googleRequestURL=[NSURL URLWithString:url];
    
            dispatch_async(kBgQueue, ^{
            NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
            [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
        });
    }
    

    -(void) queryGooglePlaces: (NSString *) googleType { } 方法中传递当前的经纬度

    【讨论】:

    • 我已经添加了它们。问题是在 [locationManager startUpdatingLocation] 之后两者都没有显示在 viewDidLoad bcoz 上;它只是去 [self queryGooglePlaces:@"doctor"];不调用 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 事件
    • 在委托方法中调用您的 -(void) queryGooglePlaces: (NSString *) googleType { }。
    • - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSString *locationLabelOne=@""; NSString *locationLabelTwo=@""; locationLabelOne=[NSString stringWithFormat:@"%f", newLocation.coordinate.latitude]; locationLabelTwo=[NSString stringWithFormat:@"%f", newLocation.coordinate.longitude]; coord.latitude = [locationLabelOne doubleValue]; coord.longitude = [locationLabelTwo doubleValue]; [自我查询GooglePlaces:@"医生"];}
    • 这样做但出现错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“数据参数为零”
    • 可能是它的 bcoz 我把半径设为 300,如果 >300 则显示错误。如何摆脱这个..?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多