开发者

iPhone: Press a button -> Centering the map

开发者 https://www.devze.com 2023-03-09 12:07 出处:网络
I hava a Google Maps implementation in my iPhone app. First, the map is set to a default location: - (void)viewDidLoad {

I hava a Google Maps implementation in my iPhone app. First, the map is set to a default location:

- (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];
}

If the user allows that his location can be used by the app, this method is called:

- (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;
    }
}

But now I have the problem, that I want to integrate a button so by pressing it, the location should be centered in the map. So if the user has allowed the appl开发者_JS百科ication to use the location, the map should be centered where the user is otherwise the default location should be used, so I can call [self moveToDefaultLocation]. How can I do this?

Also I am searching for an icon for this "user location centering" button. In this image, there is a button icon which I am looking for: http://gadgetress.freedomblogging.com/files/2008/01/iphonemapslg.jpg The icon on the left hand side of "Search", where can I get it?

Best Regards Tim.


I solved it with:

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

But I did not found this icon... Does anyone know where I can get this icon?


Glyphish has a couple of maps icons, one is like the one you linked to.

0

精彩评论

暂无评论...
验证码 换一张
取 消