Reputation: 105
In my ios application i want to place a pin on map that will always show callout by default.on that callut i am showing lat,long of pin.
What i want is pin always remain in center of map.and when user move map pin do not get moved but its lat long get changed after moving map.
i also want to show address from that lat,long.
Please guide what are the ways.
i have tried this code to
[mMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
MyAnnotation* obj = [[MyAnnotation alloc] initWithLocation:mMapView.centerCoordinate withPinType:StartPoint];
[obj setTitle:[NSString stringWithFormat:@"%f, %f",mMapView.centerCoordinate.latitude,mMapView.centerCoordinate.longitude]];
[mMapView selectAnnotation:obj animated:YES];
[mMapView addAnnotation:obj];
zoom map at current location add pin on map center showing lat long
Problem - when i move map.pin also get moved and lat long did not get changed
Upvotes: 2
Views: 2067
Reputation: 6766
In your -viewDidLoad method add following code,
[mMapView.userLocation addObserver:self
forKeyPath:@"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:NULL];
Now include the following method,
// Listen to change in the userLocation
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[object setTitle:[NSString stringWithFormat:@"%f, %f",mMapView.centerCoordinate.latitude,mMapView.centerCoordinate.longitude]];
[mMapView selectAnnotation:object animated:YES];
}
Please make sure your annotation object is class object.
Hope this will help...
Upvotes: 1