4

我想修復標記在地圖的中心,無論位置座標。如果用戶在地圖上移動相機,我希望它在中間不會出現任何閃爍的標記,並且該標記上的新位置會顯示,我該怎麼做?請幫忙。Objectivec固定標記谷歌地圖中心ios

+1

看到這個https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial –

回答

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

    } 
+1

嗨Basir,它工作正常,但如果我移動地圖,標記也移動並固定在中心。但我希望它能繼續在中心顯示出來,而不會有任何閃爍的痕跡。 – Karthick

1

爲了不移動標記

創建的ImageView或按鈕(如果可點擊)的基礎上GMSMapView中的框架上的GMSMapView中的中心。

,如果你想要得到的座標,您可以使用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 


    } 
+0

u能告訴我的目標C這行:讓centerCord = [yourMapView.camera目標] ?? – Karthick

+0

CLLocationCoordinate2D centerCord = [yourMapView.camera目標] –

+1

嗨Jasmeet,它工作正常,但如果我移動地圖,標記也移動並固定在中心。但我希望它能繼續在中心顯示出來,而不會有任何閃爍的痕跡。 – Karthick

2

嘗試以下目標C代碼

不要忘記設置Delagate

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