2012-11-10 17 views
0

這是一個有些奇怪,但我一直在試圖尋找2天的解決方案直,設置地圖區域,但似乎沒有任何工作 這裏是我的代碼:MKCoordinateRegion始終鎖定在中心座標0,0上?

-(void)viewWillAppear:(BOOL)animated{ 
//[self step1locationupdate]; 
Maplocation.showsUserLocation=YES; 

MKCoordinateRegion mapRegion; 

    mapRegion.center.longitude=self.currentProduct.shop.longcoordinate; 
mapRegion.center.latitude=self.currentProduct.shop.latcoordinate; 

NSLog(@"The USer Location are :%f",mapRegion.center.latitude); 

mapRegion.span.latitudeDelta= 2; 
mapRegion.span.longitudeDelta= 2; 

[Maplocation setRegion:mapRegion animated:YES]; 
NSLog(@"The USer Location are :%f %f",Maplocation.userLocation.coordinate.latitude,Maplocation.userLocation.coordinate.longitude); 
} 

的NSLog的爲malocation。 userlocation.coordinate在啓動時始終爲0。 我將它到viewDidLoad中部分&相同的代碼也沒有什麼區別

請幫助

回答

0

那麼我在(無效)方法已保存的上述方法中,然後我已經經過5秒從viewWillAppear中燒製方法它。

-(void)viewWillAppear:(BOOL)animated{ 
    [self performSelector:@selector(updatecurrentLocation) withObject:nil afterDelay:5]; 
} 
    -(void)updatecurrentLocation{ 

    Maplocation.showsUserLocation=YES; 
    MKCoordinateRegion mapregion; 
    mapregion.center=Maplocation.userLocation.coordinate; 
    mapregion.span.latitudeDelta = 2; 
    mapregion.span.longitudeDelta = 2; 
    [Maplocation setRegion:mapregion animated: YES]; 
} 

這就是它的鄉親:)

+0

你也切換了fr om使用'self.currentProduct.shop.longcoordinate'到'Maplocation.userLocation.coordinate'來創建要賦予'Maplocation'的區域。對我來說,這表明你的'curenntProduct.shop.longcoordinate'尚未設置。 5秒的延遲很奇怪,雖然它可能在你的測試用例中工作,但用戶位置可能需要更長時間才能在沒有良好GPS接收的其他設備上準備好。你最好從MKMapViewDelegate協議中添加一個方法,比如didUpdateUserLocation,一旦位置準備就會被調用。 – Craig

+0

嗯,我早些時候嘗試過DidUpdateUserLocation,但它從來沒有被調用過,這就是爲什麼我想出了延遲來觸發定時器的位置,因爲它自己沒有被觸發 – TALAA

+0

didUpdateUserLocation將被調用,如果你已經正確設置你的委託並告訴位置經理找到你的位置。如果您假定這個應用程序運行的每個設備在視圖出現5秒後都會有一個位置,那麼它肯定會對某些用戶造成不良影響。 – Craig

0

我認爲你需要在開始使用它之前初始化MKCoordinateRegion。像「MKCoordinateRegion mapRegion = MKMakeRegionWithCoordinates(......」

+0

嗯,我已經嘗試過了,但我有同樣的結果,我已經試過同一代碼在一個新的視圖控制器和怪異的一部分,它的工作完美。唯一不同的是,其中第一不工作有一個自定義的UITableViewCell內的地圖,第二個是在一個普通的UIViewController內 – TALAA

相關問題