2016-05-06 5 views
2

Ich muss den aktuellen Standort Benutzer pic MKView zentrieren. Aber curretnly ist es nicht in der Mitte, wie im folgenden Screenshot angegeben.So zentrieren Sie den aktuellen Standort Benutzer avtar in ios

enter image description here -

(void)locationManager:(CLLocationManager *)manager didFailWithError: (NSError *)error 
{ 
    NSLog(@"didFailWithError: %@", error); 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"didUpdateToLocation: %@", newLocation); 

    CLLocation *currentLocation = newLocation; 

    if (currentLocation != nil) 
    { 
     NSLog(@"%@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]); 

     NSLog(@"%@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]); 

     // [mkMapView setCenterCoordinate:mkMapView.userLocation.location.coordinate animated:YES]; 
    } 
} 

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 150, 150); 

    [mkMapView setRegion:[mkMapView regionThatFits:region] animated:YES]; 
} 
+0

Haben Sie den Rahmen der mkmapview zurückgesetzt, nachdem Sie die Region festgelegt haben? –

+0

Nein, ich benutze keinen Rahmen. – DJtiwari

Antwort

0

Ich habe eine Lösung. Jetzt kommt mein fb Bild in die Mitte der Karte. Wenn ich (-30, -30) -Koordinaten auf meinen profileImageView-Rahmen lege.

profileImageView.frame = CGRectMake(-30, -30, 54, 54); 

profileImageView.layer.masksToBounds = YES; 

profileImageView.layer.cornerRadius = 27; 
0
- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aLocation { 

MKCoordinateRegion region; 
MKCoordinateSpan span; 
span.latitudeDelta = 0.005; 
span.longitudeDelta = 0.005; 
CLLocationCoordinate2D location; 
location.latitude = aLocation .coordinate.latitude; 
location.longitude = aLocation .coordinate.longitude; 
region.span = span; 
region.center = location; 
[aMapView setRegion:region animated:YES]; 

}

+0

nichts anzeigen, leere Karte bekommen. – DJtiwari

0
MKCoordinateSpan span; 
MKCoordinateRegion region; 

span.longitudeDelta =0.001; 
span.latitudeDelta =0.001; 

region.span = span; 

region.center.latitude = self.mapVIew.region.center.latitude; 
region.center.longitude = self.mapVIew.region.center.longitude; 
[self.mapVIew setRegion:region]; 

+0

Benutzer avtar kommt nicht, nichts zeigen – DJtiwari

0

Verwenden Sie diesen Code Hilfe Wird,

[yourMapViewName setCenterCoordinate:yourMapViewName.userLocation.location.coordinate animated:YES]; 

oder

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 100, 100); 
    [yourMapViewName setRegion:[yourMapViewName regionThatFits:region] animated:YES]; 
} 

seine hilfreich hoffen

+0

es funktioniert nicht. – DJtiwari

+0

Wenn Sie dies oben in didUpdateToLocation: Methode verwenden? –

+0

Ja, und Sie können in meinem obigen Code sehen, ich verwende den gleichen Code – DJtiwari

0

Verwenden Sie die folgenden Delegatmethode, seine für mich arbeiten,

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { 
    //Get the east and west points on the map so you can calculate the distance (zoom level) of the current map view. 
    MKMapRect mRect = YourMapViewName.visibleMapRect; 
    MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect)); 
    MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect)); 

    //Set your current distance instance variable. 

    //Set your current center point on the map instance variable. 
    coordinate = YourMapViewName.centerCoordinate; 
} 

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:YES]; 

    self.locationManager1.distanceFilter = kCLDistanceFilterNone; 
    self.locationManager1.desiredAccuracy = kCLLocationAccuracyBest; 
    [self.locationManager1 startUpdatingLocation]; 

    //View Area 
    MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } }; 

    region.center.latitude =coordinate.latitude; 

    region.center.longitude=coordinate.longitude; 
    region.span.longitudeDelta = 0.005f; 
    region.span.longitudeDelta = 0.005f; 
    [YourMapViewName setRegion:region animated:YES]; 

} 

seine Hoffnung hilfreich

+0

Kannst du mir Klasse für currDist und koordinieren? – DJtiwari

+0

sorry currDist ist ein Int-Wert, es ist nicht notwendig zu verwenden, wenn Sie das Projekt verstecken und ausführen –

+0

nicht funktioniert, das gleiche Ergebnis. – DJtiwari

0

die Sie interessieren, wird die Hoffnung hilfreich sein.

MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } }; 
region.center.latitude = [yourLatitude doubleValue];//locationManager.location.coordinate.latitude; 
region.center.longitude = [yourLongitude doubleValue];// locationManager.location.coordinate.longitude; 
region.span.latitudeDelta = 0.005f; 
region.span.longitudeDelta = 0.005f; 
[yourMapView setRegion:region animated:YES]; 
+0

nicht in der Mitte kommen – DJtiwari