【问题标题】:How to move marker on the Google Map like Ola app如何像 Ola 应用一样在 Google 地图上移动标记
【发布时间】:2018-07-10 21:04:58
【问题描述】:

我正在开发像 Ola cabs 这样的应用程序。当用户拖动地图时,会出现一个带有标记移动的视图透明视图,当用户停止拖动时,我们必须像 ola cab 应用程序一样将 gmscamera 位置居中。这是未拖动地图时的图像。

拖动后,我使用以下代码获取位置。

    - (void) mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position {


    latitude = mapView.camera.target.latitude;
     longitude = mapView.camera.target.longitude;

    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(latitude, longitude);


     [self hourelywebsevice:updatedlogintokenstring withlat:latitude withlong:longitude withcoustamerid:updatednamestring];

   // [self hourelywebsevice:@"abc" withlat:latitude withlong:longitude];
    marker.position = center;


}

现在我的问题是如何创建第二个屏幕。

【问题讨论】:

  • 你实现过像OLA App这样的可拖动标记吗?我也在开发像 OLA 这样的应用程序。
  • 好吧,这个别针不是一个标记,而是在自己查看时添加的。而idleAtCameraPosition委托方法我们通过let coordinate = self.mapView.projection.coordinate(for: pinPointOnView)获取中心坐标。

标签: ios objective-c google-maps


【解决方案1】:

我已经在两个与车辆实时跟踪相关的应用中实现了这种功能。

我使用了一些要求:

  1. 应用具有始终位置使用权限和正确的电池使用信息。
  2. 两端都使用了 Socket 连接。司机和客户。为此使用了 Socket.io。在 Web 中配置相同。
  3. 每 2 秒触发一次定位服务,以便我可以处理电池使用情况。
  4. 每 2 秒向套接字发送一次数据,套接字侦听器可以使用该数据在地图上显示汽车。
  5. 汽车图标旋转 90 度(汽车指示向右移动)

最重要的是驾驶员的经纬度应该正确,以便在地图上显示汽车的运动。 gmsMarker 位置视为旧值,新位置值是当前位置。

let preLoc = CLLocation.init(latitude: self.gmsMarker.position.latitude, longitude: self.gmsMarker.position.longitude)
let curLoc = CLLocation.init(latitude: newLocation.latitude, longitude: newLocation.longitude)
let changeInLocation = preLoc.distance(from: curLoc)
if changeInLocation > 5{
    let degree = self.DegreeBearing(preLoc, curLoc)
    if degree > 5{
        self.gmsMarker.rotation = degree
    }
}

以下四个函数将计算您的行进方向,并根据该车在移动的道路上显示正确。

func DegreeBearing(_ A:CLLocation,_ B:CLLocation)-> Double{
    var dlon = self.ToRad(degrees: B.coordinate.longitude - A.coordinate.longitude)
    let dPhi = log(tan(self.ToRad(degrees: B.coordinate.latitude) / 2 + M_PI / 4) / tan(self.ToRad(degrees: A.coordinate.latitude) / 2 + M_PI / 4))
    if  abs(dlon) > M_PI{
        dlon = (dlon > 0) ? (dlon - 2*M_PI) : (2*M_PI + dlon)
    }
    return self.ToBearing(radians: atan2(dlon, dPhi))
}

func ToRad(degrees:Double) -> Double{
    return degrees*(M_PI/180)
}

func ToBearing(radians:Double)-> Double{
    return (ToDegrees(radians: radians) + 360).truncatingRemainder(dividingBy: 360)
}

func ToDegrees(radians:Double)->Double{
    return radians * 180 / M_PI
}

对于客户方,只需将您从驱动程序方收到的任何数据显示为 self.gmsMarker.rotation 值。

【讨论】:

  • @patel 感谢您的快速响应,我不会快速语言,也不想计算距离,我的要求是当用户滚动地图视图时,您应该显示一个透明视图,例如 ola app 主屏幕停止滚动 tanspernt 视图必须关闭并且标记应该像 ola 一样放置在 mapview 上
  • 嗨@SatheeshkumarNaidu 对于地图移动,谷歌已经有那种方法可以让你获得拖动地图事件。 (void) mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker; (void) mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker;
【解决方案2】:

使用 GMSMapView 的委托

- (void) mapView:(GMSMapView *)mapView didDragMarker:(GMSMarker *)marker;
- (void) mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker;
- (void) mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker;

【讨论】:

  • 嗨 Garg,感谢您的快速响应,如果用户拖动地图,则调用哪个方法。
  • 上面给出的方法会被调用,但是你可以在这个方法中设置你的坐标 -(void) mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker{ NSLog(@"marker dragged to位置:%f,%f",marker.position.latitude,marker.position.longitude); }
  • 该方法没有被调用,如果看到旧应用主屏幕,您可以轻松理解我的要求。
  • @SatheeshkumarNaidu 你设置了代表,即 mapView_.delegate = self; marker_.draggable = YES;
  • @SatheeshkumarNaidu 你试过这些代表吗 - (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture; (void) mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position
【解决方案3】:
@interface ViewController () <CLLocationManagerDelegate, GMSMapViewDelegate> {
    GMSMarker *marker2;
}

在 viewDidLoad 中

marker2 = [[GMSMarker alloc] init];

// Create a GMSCameraPosition that tells the map to display the
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
                                                        longitude:longitude
                                                             zoom:18];
// Create GMSMapView
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

// Available map types: kGMSTypeNormal, kGMSTypeSatellite, kGMSTypeHybrid,
// kGMSTypeTerrain, kGMSTypeNone

// Set the mapType to Satellite
mapView.mapType = kGMSTypeSatellite;


mapView.myLocationEnabled = YES;
self.view = mapView;

// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(latitude, longitude);
marker.map = mapView;

mapView.delegate = self;

之后调用委托函数

- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture {
//    NSLog(@"willMove");
}

- (void) mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
//    NSLog(@"didChangeCameraPosition");

// Creates a marker in the center of the map.

//    NSLog(@"%@", position);
      marker2.position = CLLocationCoordinate2DMake(mapView.camera.target.latitude, mapView.camera.target.longitude);
      marker2.map = mapView;
//    marker2.draggable = YES;
      marker2.icon = [GMSMarker markerImageWithColor:[UIColor blueColor]];


}

【讨论】:

    猜你喜欢
    • 2017-01-15
    • 1970-01-01
    • 2016-09-22
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    相关资源
    最近更新 更多