Gaurav Rami
Gaurav Rami

Reputation: 1287

Get latitude and longitude of center of half of mapView gooleMap (GMS MapView) ios

I am using GMS MapView (Google maps for iOS). I have four corners of a mapView as well as center position (lat. & long.) of the mapView but actually I want to logically divide my mapView in two parts, i.e. top and bottom and then I want to get center point (lat. & long.) of both parts.

Please see the attached images (I want to find the location(lat & long) of RED Dots).

enter image description here

and

enter image description here

How can I do that?

Its so kind of you if you answer it.

Thank you.
Gerry

Upvotes: 1

Views: 1119

Answers (1)

Guy S
Guy S

Reputation: 1424

according to the image you provided you are dividing the screen in the middle.

try this:

    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    CGFloat middleOfScreenX = [UIScreen mainScreen].bounds.size.width/2;
    CGFloat pointYouDivideScreen = [UIScreen mainScreen].bounds.size.height/2; // this is in order to divide the map exactly in the middle, you can change as you wish



    CGPoint partOneCenterPoint = CGPointMake(middleOfScreenX, pointYouDivideScreen/2);
    CLLocationCoordinate2D partOneCenter = [self.map.projection coordinateForPoint:partOneCenterPoint];

    CGPoint partTwoCenterPoint = CGPointMake(middleOfScreenX, screenHeight - (screenHeight - pointYouDivideScreen)/2);
    CLLocationCoordinate2D partTwoCenter = [self.map.projection coordinateForPoint:partTwoCenterPoint];


    GMSMarker *marker1 = [[GMSMarker alloc] init];
    marker1.position = partOneCenter;
    marker1.map = self.mapView;

    GMSMarker *marker2 = [[GMSMarker alloc] init];
    marker2.position = partTwoCenter;
    marker2.map = self.mapView;

the markers are placed in the center of each part (just to show the centers)

you can get lat/lng of each center with:

partOneCenter.latitude;
partOneCenter.longitude;

partTwoCenter.latitude;
partTwoCenter.longitude;

Upvotes: 0

Related Questions