【问题标题】:need to drop pin at two places... current location and events locations需要在两个地方放置大头针……当前位置和活动位置
【发布时间】:2010-12-20 21:56:19
【问题描述】:

我需要这方面的帮助....放下别针。

当前位置.... pin drop.... 与蓝色.... 活动地点:locations latitude:53.373812...longitude 4.890951,带红色别针。

我是这样做的:

@interface AddressAnnotation : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;

    NSString *mTitle;
    NSString *mSubTitle;

//  CLLocationManager *locationManager;
//  CLLocation *currentLocation;
}
@end
@interface MapViewController : UIViewController <CLLocationManagerDelegate>  {

    IBOutlet MKMapView *mapView;    
    AddressAnnotation *addAnnotation;
    NSString *address;
    CLLocationManager *locationManager;
    CLLocation *currentLocation;
}
+(MapViewController *)sharedInstance;
-(void)start;
-(void)stop;
-(BOOL)locationKnown;
@property(nonatomic,retain)CLLocation *currentLocation;
@property(nonatomic,retain)NSString *address;
-(CLLocationCoordinate2D) addressLocation;
-(void)showAddress;

@end

//实现文件。

#import "MapViewController.h"



@implementation AddressAnnotation
@synthesize coordinate;
//@synthesize currentLocation;

- (NSString *)subtitle{
    //return @"Sub Title";
    return @"Event";
}
- (NSString *)title{
    //return @"Title";
    return @"Location ";
}

-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
    coordinate=c;
    //NSLog(@"%f,%f",c.latitude,c.longitude);
    return self;
}

@end

@implementation MapViewController
@synthesize address;
@synthesize currentLocation;

static MapViewController *sharedInstance;

+(MapViewController *)sharedInstance{
    @synchronized (self)
    {
        if (!sharedInstance) 
            [[MapViewController alloc]init];
        }
    return sharedInstance;
}
+(id)alloc{
    @synchronized(self){
        NSAssert(sharedInstance==nil,"Attempted to allocate a second instance of a singleton LocationController."); 
        sharedInstance = [super alloc];
    }
    return sharedInstance;
}
-(id)init{
    if(self==[super init]){
        self.currentLocation=[[CLLocation alloc]init];
        locationManager=[[CLLocationManager alloc]init];
        locationManager.delegate=self;
        [self start];
    }
    return self;
}
-(void)start{
    NSLog(@"Start");
    [locationManager startUpdatingLocation];
}
-(void)stop{
    [locationManager stopUpdatingLocation];
}
-(BOOL)locationKnown{
    if (round(currentLocation.speed)==-1) 
        return NO;
        else return YES;

}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    if (abs([newLocation.timestamp timeIntervalSinceDate:[NSDate date]])<120){
        self.currentLocation=newLocation;
    }
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    UIAlertView *alert;
    alert=[[UIAlertView alloc]initWithTitle:@"Error" message:[error description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
} 
        // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"Map-View";
    [self addressLocation];
    [self showAddress];
    NSLog(@"address is %@",address);

}

-(void)showAddress{ 

    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta=0.2;
    span.longitudeDelta=0.2;

    CLLocationCoordinate2D location = [self addressLocation];
    region.span=span;
    region.center=location;

    if(addAnnotation != nil) {
        [mapView removeAnnotation:addAnnotation];
        [addAnnotation release];
        addAnnotation = nil;
    }

    addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
    [mapView addAnnotation:addAnnotation];

    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];

}


-(CLLocationCoordinate2D) addressLocation {

    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 

   [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
    NSArray *listItems = [locationString componentsSeparatedByString:@","];

    double latitude = 0.0;
    double longitude = 0.0;

    if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
        latitude = [[listItems objectAtIndex:2] doubleValue];
        longitude = [[listItems objectAtIndex:3] doubleValue];
    }
    else {
        //Show error
    }
    CLLocationCoordinate2D location;
    location.latitude = latitude;
    location.longitude = longitude;

    return location;
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.pinColor = MKPinAnnotationColorRed;
    annView.animatesDrop=YES;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [address release];
    [super dealloc];
}


@end

请帮帮我...

提前致谢。

【问题讨论】:

    标签: iphone


    【解决方案1】:

    显示用户当前位置很简单。

    -(void)start{
        NSLog(@"Start");
         mapView.showsUserLocation=YES; //This will show the current location as blue dot in your mapview
        [locationManager startUpdatingLocation];
    }
    -(void)stop{
         mapView.showsUserLocation=NO;
        [locationManager stopUpdatingLocation];
    }
    

    在你看来ForAnnotation Delegate

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if (annotation == mapView.userLocation) 
    {
        // This code will execute when the current location is called.
        return nil;
    }
    else
    {
        MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
        annView.pinColor = MKPinAnnotationColorRed;
        annView.animatesDrop=YES;
        annView.canShowCallout = YES;
        annView.calloutOffset = CGPointMake(-5, 5);
        return annView;
    }
    

    【讨论】:

    • 如果条件我需要用不同的颜色为我当前的位置调用其他引脚,它的工作先生........ MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier :@"currentloc"]; annView.pinColor = MKPinAnnotationColorGreen; annView.animatesDrop=YES; annView.canShowCallout = 是; annView.calloutOffset = CGPointMake(-5, 5);返回安视图;
    • 我在我的设备上测试了这个应用程序......它只指向我的目标位置......有没有人帮我弄清楚该怎么做......当我的视图加载时,我需要指出我当前的位置位置......我也需要在目的地放下别针......提前帮助我@thanks
    • 请看我上面的代码。当您将 showUserLocation 设置为 YES 时,操作系统将负责注释显示用户当前位置的蓝点。不要在 if 条件内创建任何注释视图。只需在此处返回 nil。
    • @build 成功了,当我完成此操作时,我无法在当前位置看到模拟器中的引脚。我通过了 - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation{ if (annotation==mapView.userLocation) { return nil; // } else { MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"]; annView.pinColor = MKPinAnnotationColorRed; annView.animatesDrop=YES; annView.canShowCallout = 是; annView.calloutOffset = CGPointMake(-5, 5);返回安视图; } }
    • 我想我出局了。看到这个链接forums.macrumors.com/showthread.php?t=759744
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多