【问题标题】:Objectivec fixed marker on center of google map ios谷歌地图ios中心的Objective C固定标记
【发布时间】:2017-08-30 13:37:50
【问题描述】:

无论位置坐标如何,我都想将标记固定在地图的中心。如果用户在地图上移动相机,我希望它继续显示在中心而没有任何闪烁的标记和该标记显示的新位置,我该怎么做?请帮忙。

【问题讨论】:

标签: ios objective-c iphone google-maps-markers google-maps-sdk-ios


【解决方案1】:

试试下面的 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];

然后使用谷歌地图的委托方法

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

【讨论】:

    【解决方案2】:

    试试下面的代码

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

    【讨论】:

    • 嗨,Basir,它工作正常,但如果我移动地图,标记也会移动并固定在中心。但我希望它继续显示在中心,而不会在标记中闪烁。
    【解决方案3】:

    不移动标记

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

    如果你想获取坐标,你可以使用mapView.projection.coordinateForPoint

    这将移动标记

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

    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
    
    
        }
    

    【讨论】:

    • 你能告诉我目标 c 中的这一行吗:let centerCord = [yourMapView.camera target] ??
    • CLLocationCoordinate2D centerCord = [yourMapView.camera target]
    • 嗨 Jasmeet,它工作正常,但如果我移动地图,标记也会移动并固定在中心。但我希望它继续显示在中心,而不会在标记中闪烁。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 2014-07-27
    相关资源
    最近更新 更多