【问题标题】:showing current location ,along with another location in iphone显示当前位置,以及 iphone 中的另一个位置
【发布时间】:2010-05-07 08:54:39
【问题描述】:

每当我们加载地图视图时,它都会自动显示当前用户的位置。在我的情况下,我在地图上显示另一个位置,这样地图会加载当前位置并缩放到我给出的位置..但是,当前位置点没有显示(自动出现的蓝色位置..)。 我给了 mapView.showCurrentLocation=TRUE;但它没有显示。 所以任何人都可以告诉它的工作方式,它应该说当前位置标记,然后缩放到我给出的点。 谢谢

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    static NSString *defaultPinID = @"CameraAnnotation";
    MKPinAnnotationView *retval = nil;
    static int postag=1;

    (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if(annotation=MapView.userLocation)
    {
        return nil;
    }
    // If we have to, create a new view
    else    if (retval == nil)
    {
        retval = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        myDetailButton.frame = CGRectMake(0, 0, 50, 23);
        myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        myDetailButton.tag=postag;
        postag++;
        [myDetailButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];
        retval.rightCalloutAccessoryView = myDetailButton;
        [retval setPinColor:MKPinAnnotationColorRed];
        retval.animatesDrop = YES;
        retval.canShowCallout = YES;
    }

    return retval;
}

【问题讨论】:

  • 您是在签入模拟器还是设备?

标签: iphone cocoa-touch mapkit


【解决方案1】:

在模拟器中,默认的当前位置是库比蒂诺。这是您所看到的,它将在设备中正常工作。

您应该使用 CLLocationManager 来获取当前位置。

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
{
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self; // Tells the location manager to send updates to this object
[locationManager startUpdatingLocation];
}

更改图钉颜色

MKPinAnnotationView   *pin=[[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"]autorelease];

[pin setPinColor:MKPinAnnotationColorRed];

一切顺利。

【讨论】:

  • 感谢您的回复,但我仅将其用于显示其他位置。但我的问题是地图直接放大到该位置。所以当前位置不直接可见。我正在尝试增加跨度值的一些实现方式,但不知道它是否正确,谢谢....
  • 查看此链接以使用缩放级别。 troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview 。如果您只想在地图中显示一个位置,您可以根据您的要求手动调整跨度值。
  • 谢谢您,将使用它。我目前遇到了另一个问题。也就是说,我在此处找到了另一个位置以及当前位置,并为这些位置添加了注释,以便用户可以去单击注释时转到另一个屏幕。但我的问题是注释也来到了当前位置..我想你有我的问题和引脚颜色我也需要为他们显示不同的(红色表示自定义引脚,蓝色表示用户当前位置)..抱歉麻烦,非常感谢
  • 非常感谢 Warrior 的回复和耐心。你的回答是对的,但你不明白我想要自定义视图的要求。也就是说,当前位置应该是蓝色的 pin,另一个是红色的。 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation { } 在上述方法中,我们如何识别哪个是当前位置,哪个是我们正在显示的位置..跨度>
  • 查看此堆栈溢出答案stackoverflow.com/questions/1853144/…
【解决方案2】:

查看此问题上的 answer 以设置 MkMapView 缩放级别以包含附加到它的所有 MKAnnotations

【讨论】:

  • 谢谢,我已经通过增加我显示的位置的跨度值来设置预期当前位置。一些他们是如何靠近的。
猜你喜欢
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多