2010-12-21 7 views
0

緑のピンと目的地の位置が赤いピンで現在の位置を取得するにはどうすればよいですか?現在の位置と目的地のピンを取得する必要があります

私はいくつかのものに取り組むとき、私は現在の場所ではなく、赤いピンで目的地の場所だけを取得します。

私のソースコード。

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 
#import <MapKit/MKAnnotation.h> 
#import <CoreLocation/CoreLocation.h> 

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

    NSString *mTitle; 
    NSString *mSubTitle; 

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

    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 @"Allure-Exclusive"; 
} 

-(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"); 
    mapView.showsUserLocation=YES; 
    [locationManager startUpdatingLocation]; 
} 
-(void)stop{ 
    mapView.showsUserLocation=NO; 
    [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]; 
    [email protected]"Map-View"; 
// [self addressLocation]; 
    [self showAddress]; 
    NSLog(@"address is %@",address); 

} 

-(void)showAddress{ 

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

    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]]; 
    NSLog(@"locationString %@",locationString); 
    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]; 
     NSLog(@"listItems %@",[listItems objectAtIndex:2]); 
    } 
    else { 
     //Show error 
    } 
    CLLocationCoordinate2D location; 
    location.latitude = latitude; 
    location.longitude = longitude; 

    return location; 
} 

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{ 
    if (annotation==mapView.userLocation) { 
     MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"]; 
     annView.pinColor = MKPinAnnotationColorGreen; 
     annView.animatesDrop=YES; 
     annView.canShowCallout = YES; 
     annView.calloutOffset = CGPointMake(-5, 5); 
     return annView; 
      // 
} 
    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; 
} 



} 


- (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 { 
// [self stop]; 
    [super viewDidUnload]; 

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


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

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

    if (annotation==mapView.userLocation) 
    { 
    [email protected]"Current Location"; 
    [mapView setRegion:MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 1000, 1000)animated:YES]; 
    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; 

} 
} 

私はこのメソッドを変更しました。それは青く点滅していますが、無限Loop mariani Aveの場所にある別の場所を指していました。

これはシミュレータで実行されていました。

答えて

1

showsUserLocationを設定します。

mapView.showsUserLocation = YES;

+0

私はイエス=のviewDidLoadのmapview.showsuserlocationであることを設定します。しかし、それは先生を示さない。 –

関連する問題