iphone 不显示当前位置

发布于 2024-11-23 19:51:36 字数 1226 浏览 0 评论 0原文

我需要有关 iphone MapKit 的帮助! 我使用界面生成器创建了一个简单的地图视图,并将“显示用户位置”设置为“是”。 然而,“蓝点”并没有出现在我部署的 itouch 上!我需要帮助!

另外,任何人都可以建议我如何获取用户当前位置并绘制到另一个位置的路线? 有没有像苹果地图应用那样的官方API供我们使用?来绘制路线。

太感谢了!

我的代码。

viewDidLoad

- (void)viewDidLoad {
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager startUpdatingLocation];
    [super viewDidLoad];
}

locationManager:didUpdateToLocation:fromLocation:

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

    [mapView setMapType:MKMapTypeStandard];
    mapView.showsUserLocation = YES;

    MKCoordinateRegion region;

    CLLocationCoordinate2D location;
    location.latitude = newLocation.coordinate.latitude;
    location.longitude = newLocation.coordinate.longitude;

    region.center = location;
    region.span.longitudeDelta = 0.02;
    region.span.latitudeDelta = 0.02;
    [mapView setRegion:region animated:YES]; 
    [mapView setDelegate:self];
}

I need help on iphone MapKit!
I created a simple mapview using interface builder and set the show user location to yes.
However, the "blue dot" is not showing up on my deployed itouch! I need help on it!

Also, anyone can advise how can i get the user current location and plot a route to another location?
Is there no official API for us to use like how apple's map app used ? To draw the route.

Thank you so much!

My code.

viewDidLoad

- (void)viewDidLoad {
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager startUpdatingLocation];
    [super viewDidLoad];
}

locationManager:didUpdateToLocation:fromLocation:

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

    [mapView setMapType:MKMapTypeStandard];
    mapView.showsUserLocation = YES;

    MKCoordinateRegion region;

    CLLocationCoordinate2D location;
    location.latitude = newLocation.coordinate.latitude;
    location.longitude = newLocation.coordinate.longitude;

    region.center = location;
    region.span.longitudeDelta = 0.02;
    region.span.latitudeDelta = 0.02;
    [mapView setRegion:region animated:YES]; 
    [mapView setDelegate:self];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ま柒月 2024-11-30 19:51:37

对于第 2 点:如果您希望使用地图显示路线(以便您的应用程序进入后台),请使用:

NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",fromLocation.latitude, fromLocation.longitude,toLocation.latitude,toLocation.longitude];
            [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

如果您想在 MKMapview 中绘制路线,我建议您查看此 https://github.com/kishikawakatsumi/MapKit-Route-Directions

不知道第一点,您是否在“设置”中激活了定位服务?

For point 2: If you want the show the route with Maps (so your application goes in background) use:

NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",fromLocation.latitude, fromLocation.longitude,toLocation.latitude,toLocation.longitude];
            [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

If you want to draw the route in your MKMapview i suggest you to look at this https://github.com/kishikawakatsumi/MapKit-Route-Directions.

I don't know for the point 1, have you activated location services in Settings?

中性美 2024-11-30 19:51:37

你在下面的方法中写了什么?它负责在用户当前位置上放置蓝点或图钉。

注意:我只是复制粘贴我的代码,因此您需要根据需要修改它。简而言之,如果注释是用户的当前位置,则只需返回 view nil 即可。然后它会自动在用户当前位置的中心有一个蓝点。

- (MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *view = nil;

if(annotation !=mapview.userLocation){
    view = (MKPinAnnotationView *) 
    [mapview dequeueReusableAnnotationViewWithIdentifier:@"identifier"]; 
    if(nil == view) { 
        view = [[[MKPinAnnotationView alloc] 
                 initWithAnnotation:annotation reuseIdentifier:@"identifier"] 
                autorelease];           
    }
    [view setPinColor:MKPinAnnotationColorPurple];
    UIButton *btnViewVenue = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    btnViewVenue.frame = CGRectMake(0, 0, 30, 30);
    view.rightCalloutAccessoryView=btnViewVenue;
    view.pinColor = MKPinAnnotationColorGreen;
    view.enabled = YES;
    view.canShowCallout = YES;
    view.multipleTouchEnabled = NO;
    view.animatesDrop = YES;
    }       
return view;
}

对于第二点,绘制两个位置之间的路线。因此,据我所知,在您手动绘制线条并且拥有所有路线的坐标之前,正式不可能在 MapKit 中显示两个位置之间的路线。

希望这有帮助。

What did you write in below method ? It is responsible to place blue dot or pin on the user current location.

Note: I just copy paste my code so you need to modify it as per your need. In short you just need to return view nil if annotation is user's current location. Then It will have blue dot in center in user's current location automatically.

- (MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *view = nil;

if(annotation !=mapview.userLocation){
    view = (MKPinAnnotationView *) 
    [mapview dequeueReusableAnnotationViewWithIdentifier:@"identifier"]; 
    if(nil == view) { 
        view = [[[MKPinAnnotationView alloc] 
                 initWithAnnotation:annotation reuseIdentifier:@"identifier"] 
                autorelease];           
    }
    [view setPinColor:MKPinAnnotationColorPurple];
    UIButton *btnViewVenue = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    btnViewVenue.frame = CGRectMake(0, 0, 30, 30);
    view.rightCalloutAccessoryView=btnViewVenue;
    view.pinColor = MKPinAnnotationColorGreen;
    view.enabled = YES;
    view.canShowCallout = YES;
    view.multipleTouchEnabled = NO;
    view.animatesDrop = YES;
    }       
return view;
}

For your 2nd Point about draw route between two location. So as per my knowledge it is not officially possible to show route between two location in MapKit until you draw lines manually and if you have co-ordinates for all the routes.

Hope this help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文