Search code examples
ios5mkmapviewuserlocation

How can I set mapView's userLocation to a MKUserLocation that I created?


Im trying to do some coding about improving the iPhone positioning accuracy. I got all needed info(e.g. latitude, longitude, deviceMotion, Gyro..) from my iPhone hardware and need to display the more accurate calculation result on the mapView. But just then I found that the userLocation property of the mapView is readonly, so it probably means it cannot be changed even use the setCoordinate method, so sad =[ Is there any solution that can deal with it or there has another way to display that position on my map view? Thanks!


Solution

  • You just need to create CLLocationCoordinate2D with CLLocationCoordinate2DMake and set there latitude a longitude. Then create your position as new MKAnotation (for example yourPositionAnotation) and set there your position from CLLocationCoordinate2D as a coordinate. Then do just something like this:

    // create coordinate
    CLLocationCoordinate2D yourPos = CLLocationCoordinate2DMake(lat, lon);
    // create MKAnnotation
    MKAnnotationView *yourAnnotation = [[MKAnnotationView alloc] init];
    
    yourAnnotation.coordinate = yourPos;
    
    // remove all old annotations
    [self.myMapView removeAnnotations:self.myMapView.annotations]
    // add your annotation
    [self.myMapView addAnnotation:yourAnnotation];
    

    Don't forget release annotation in the end.