shilparamesh shilparamesh - 5 months ago 213
Swift Question

how to move the maker along with moving of google map in ios?

I am displaying marker in some particular place...along with displaying the current address in label on google map....now i want to change the location by moving the google map...but the problem is when am moving the map simultaneously i should move the marker along with the map..and i should display the address of that location in address label..can anyone help to achieve this...
I tried like this:
let destinationMarker = GMSMarker(position: self.destinationLocation.coordinate)

let image = UIImage(named:"sourcemarker")
destinationMarker.icon = image
destinationMarker.draggable = true
destinationMarker.map = self.viewMap
//viewMap.selectedMarker = destinationMarker
destinationMarker.title = "hi"
destinationMarker.userData = "changedestination" func mapView(mapView: GMSMapView, didEndDraggingMarker marker: GMSMarker) {
if marker.userData as! String == "changedestination"
{
self.destinationLocation = CLLocation(latitude: marker.position.latitude, longitude: marker.position.longitude)
self.destinationCoordinate = self.destinationLocation.coordinate
//getAddressFromLatLong(destinationCoordinate)






}
}


thanks in advance...

Answer

There is one trick that can help you out here. Instead of using a GMSMarker here, put an image pointing to the center, over your Google MapView.

You can easily find the coordinates of Map's center using this :

double latitude = mapView.camera.target.latitude;
double longitude = mapView.camera.target.longitude;

Or this

GMSCoordinateBounds *bounds = nil;
bounds = [[GMSCoordinateBounds alloc] initWithRegion: visibleRegion];

CLLocationCoordinate2D centre = CLLocationCoordinate2DMake(
                                                           (bounds.southWest.latitude + bounds.northEast.latitude) / 2,
                                                           (bounds.southWest.longitude + bounds.northEast.longitude) / 2);

Now you can get the location address by using Geocoding API by google.

Here is the reference : https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_geocoder

You can refresh Address when this delegate method is called :

- (void) mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position 

Hope this helps.