我正在开发类似Ola cabs的应用程序。当用户拖动地图时,视图透明视图随着标记移动而出现,当用户停止拖动时,我们必须将gmscamera位置居中,就像ola cab app一样。这是未拖动地图时的图像。
拖动后我使用以下代码获取位置。
- (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;
}
现在我的问题是如何创建第二个屏幕。
答案 0 :(得分:2)
使用GMSMapView的代表
- (void) mapView:(GMSMapView *)mapView didDragMarker:(GMSMarker *)marker;
- (void) mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker;
- (void) mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker;
答案 1 :(得分:2)
我在两个与vahicle的实时跟踪相关的应用程序中实现了这种功能。
我使用了一些要求:
最重要的是驾驶员经度和经度应该正确显示地图上的汽车运动。 gmsMarker位置视为旧值,newLocation值是当前位置。
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
值。
答案 2 :(得分:-1)
@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]];
}