iOS中Google地图上居中的Objective-C固定标记

6

我希望无论地图的位置坐标如何,都能将标记固定在地图中心。如果用户在地图上移动相机,我希望它仍然显示在中心位置,而不会闪烁,并且该标记上的新位置也会显示出来。我该怎么做?请帮忙。


1
请查看此链接:https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial - Anbu.Karthik
3个回答

5
尝试以下Objective C代码:
``` //请勿忘记设置代理 ```
请注意,上述内容已包含HTML标签,并已翻译成通俗易懂的语言。
  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;
     }

3

如何使标记不移动

基于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


    }

你能告诉我这行Objective-C代码的含义吗:let centerCord = [yourMapView.camera target]? - Karthick
CLLocationCoordinate2D centerCord = [yourMapView.camera target] CLLocationCoordinate2D centerCord = [yourMapView.camera target] - Jasmeet Kaur
1
嗨Jasmeet,它的运行很好,但是如果我移动地图,标记也会移动并固定在中心位置。但是我希望它可以在中心位置持续显示而没有任何标记闪烁问题。 - Karthick

3
尝试以下代码:

尝试以下代码

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

    }

1
嗨Basir,它运行良好,但如果我移动地图,标记也会移动并固定在中心。但我希望它保持在中心显示,而不会出现标记闪烁的情况。 - Karthick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接