2011-05-30 66 views
1

我在我的iPhone應用程序中使用Google Maps實現。首先,地圖設置爲默認位置:iPhone:按下按鈕 - >居中地圖

- (void)viewDidLoad { 
    firstTimeUserLocationCentered = YES; 
    [self moveToDefaultLocation]; 
    firstTimeUserLocationCentered = NO; 
    ... 
} 

- (void)moveToDefaultLocation { 
    MKCoordinateRegion myDefaultRegion; 
    myDefaultRegion.center.latitude = defaultLocLat; 
    myDefaultRegion.center.longitude = defaultLocLong; 
    myDefaultRegion.span.latitudeDelta = defaultLocLatDelta; 
    myDefaultRegion.span.longitudeDelta = defaultLocLongDelta; 
    [self.myMapView setRegion:myDefaultRegion animated:YES]; 
} 

如果用戶允許,他的位置可以由應用程序使用,這種方法被稱爲:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { 
    if (!firstTimeUserLocationCentered) { 
     MKCoordinateRegion myUserRegion; 
     myUserRegion.span.latitudeDelta = userLocLatDelta; 
     myUserRegion.span.longitudeDelta = userLocLongDelta; 
     myUserRegion.center = userLocation.coordinate; 
     [self.myMapView setRegion:myUserRegion animated:YES]; 
     firstTimeUserLocationCentered = YES; 
    } 
} 

但現在我有問題,我想集成一個按鈕,所以按下它,該位置應該在地圖中居中。因此,如果用戶允許應用程序使用該位置,則地圖應該位於用戶所在位置,否則應該使用默認位置,因此我可以撥打[self moveToDefaultLocation]。 我該怎麼做?

另外我正在搜索此「用戶位置居中」按鈕的圖標。在這個圖像中,我正在尋找一個按鈕圖標:http://gadgetress.freedomblogging.com/files/2008/01/iphonemapslg.jpg「搜索」左側的圖標,我可以在哪裏找到它?

最好的問候蒂姆。

回答

2

我解決了它:

MKUserLocation *myUserLocation = self.myMapView.userLocation; 
if (!myUserLocation.location) { 
    [self moveToDefaultLocation]; 
} else { 
    self.mapView.centerCoordinate = self.mapView.myUserLocation.location.coordinate; 
} 

但我沒發現這個圖標...有誰知道我能得到這個圖標?

+2

使用'setCenterCoordinate:animated:'獲取動畫(並使其與默認位置方法類似)。 – 2011-05-30 12:48:44

0

Glyphish有幾個地圖圖標,其中一個就像你鏈接到的那個。