Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objectivec fixed marker on center of google map ios

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.

like image 861
Karthick Avatar asked Apr 05 '17 05:04

Karthick


People also ask

How do I add a marker to Google Maps Center?

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.


2 Answers

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;
     }
like image 96
Saifan Nadaf Avatar answered Sep 17 '22 14:09

Saifan Nadaf


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 ;)

    }
like image 29
Basir Alam Avatar answered Sep 17 '22 14:09

Basir Alam