I want to fix the marker in the center of the map irrespective of the location coordinates. If the user moves the camera on map I want it to keep showing up in the center without any flickering in marker and new location on that marker shows, How can I do that? Please help out.
In the "Addmarker" function in your while... end loop you should be building Marker objects using the Google Maps API. You may need to assign each marker (or just the marker(s) you wish to work with) to an array so that you can access them.
Try The Below Objective C Code
Dont Forget To Set Delegate
mapView.delegate=self;
Create ImageView And Add It To The Center Of The Map
UIImageView *pin =[[UIImageView alloc]init];
pin.frame=CGRectMake(0, 0, 20, 20 );
pin.center = mapView.center;
pin.image = [UIImage imageNamed:@"location.png"];
[self.view addSubview:pin];
[self.view bringSubviewToFront:pin];
Then Use Delegate Method Of Google Map
//When You Will Scroll Map Then This Method Will Be Called
- (void)mapView:(GMSMapView *)MapView didChangeCameraPosition:(GMSCameraPosition *)position {
// Get Latitude And Longitude Of Your Pin(ImageView)
CLLocationCoordinate2D newCoords = CLLocationCoordinate2DMake( position.target.latitude , position.target.longitude);
NSLog(@"Latitude-%f\Longitude-%f\n",newCoords.latitude, newCoords.longitude);
[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(newCoords.latitude, newCoords.longitude) completionHandler:^(GMSReverseGeocodeResponse* response, NSError* error) {
//Get Place Details
NSLog(@"%@",[[response results]objectAtIndex:0].lines);
}];
return;
}
Try the below code
GMSCameraPosition *cameraPosition;
- (void)mapView:(GMSMapView *)pMapView didChangeCameraPosition:(GMSCameraPosition *)position {
/* move draggable pin */
if (fixedMarker) {
// stick it on map and start dragging from there..
if (lastCameraPosition == nil) lastCameraPosition = position;
// Algebra :) substract coordinates with the difference of camera changes
double lat = position.target.latitude - lastCameraPosition.target.latitude;
double lng = position.target.longitude - lastCameraPosition.target.longitude;
lastCameraPosition = position;
CLLocationCoordinate2D newCoords = CLLocationCoordinate2DMake(fixedMarker.googleMarker.position.latitude+lat,
fixedMarker.googleMarker.position.longitude+lng);
[fixedMarker.googleMarker setPosition:newCoords];
return;
}
}
- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position {
cameraPosition = nil; // reset pin moving, no ice skating pins ;)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With