google 地图 ios 中心的 Objective C 固定标记

Objectivec fixed marker on center of google map ios

我想将标记固定在地图的中心,而不考虑位置坐标。如果用户在地图上移动相机,我希望它一直显示在中心,而标记不会闪烁,并且该标记会显示新位置,我该怎么做?请大家帮忙

不移动标记

基于GMSMapView 的框架在GMSMapView 的中心创建一个Imageview 或Button(如果可点击)。

如果你想得到坐标你可以使用mapView.projection.coordinateForPoint

这将移动标记

通过以下代码找到 google 地图的中心点

let centerCord = yourMapView.camera.target 

let marker = GMSMarker(position: centerCord)
marker.title = "Hello World"
marker.map = mapView

实施mapView:didChangeCameraPosition:

 func mapView(mapView: GMSMapView, didChangeCameraPosition position: GMSCameraPosition) {
     // to remove all markers       
     mapView.clear()

    //create new marker with new center
    let centerCord = [yourMapView.camera target] 

    let marker = GMSMarker(position: centerCord)
    marker.title = "Hello World"
    marker.map = mapView


    }

试试下面的代码

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

    }

试试下面的 Objective C 代码

别忘了设置委托

  mapView.delegate=self;

创建 ImageView 并将其添加到地图的中心

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

然后使用Google映射的委托方法

//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;
     }