6

我目前正在构建一个 ios 应用程序,我希望实现一个功能,其中用户的位置显示在谷歌地图视图上,当他们移动折线时,显示用户到目前为止所走过的路径。这显然需要实时发生。

到目前为止,我已经初始化了一个谷歌地图视图,并且可以使用observeValueForKeyPath函数显示用户的当前位置。视图和位置标记会随着用户移动而更新。

我认为最好的方法是创建一个 GMSMutablePath 对象,该对象在每次地图相机更新到新位置时添加用户的当前位置,然后从该路径创建一条折线。我用于此的代码如下:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]])
    {
        [self.myMapView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:self.myMapView.myLocation.coordinate.latitude
                                                                                 longitude:self.myMapView.myLocation.coordinate.longitude
                                                                                      zoom:18]];


        [trackedPath addCoordinate:CLLocationCoordinate2DMake(self.myMapView.myLocation.coordinate.latitude, self.myMapView.myLocation.coordinate.longitude)];
        GMSPolyline *testPoly = [GMSPolyline polylineWithPath:trackedPath];
        testPoly.strokeWidth = 8;
        testPoly.strokeColor = [UIColor redColor];
        testPoly.map = myMapView;
    }
}

在实践中,这不会在地图上产生任何折线,因此非常感谢任何帮助/建议!

4

2 回答 2

8
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{  NSString *pointString=[NSString    stringWithFormat:@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude];
    [self.points addObject:pointString];
GMSMutablePath *path = [GMSMutablePath path];
for (int i=0; i<self.points.count; i++)
{           
  NSArray *latlongArray = [[self.points   objectAtIndex:i]componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];

[path addLatitude:[[latlongArray objectAtIndex:0] doubleValue] longitude:[[latlongArray objectAtIndex:1] doubleValue]];
 }

if (self.points.count>2)
{
    GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
    polyline.strokeColor = [UIColor blueColor];
    polyline.strokeWidth = 5.f;
    polyline.map = mapView_;
    self.view = mapView_;
}}
于 2013-11-05T10:09:16.493 回答
2

如果你想画两条路径顺便说一句两个地方使用这个方法。它的演示快速方法可以找到两个地方的溃败。

//MARK: API CALL
func GetRoughtofTwoLocation(){
    let originString: String = "\(23.5800),\(72.5853)"
    let destinationString: String = "\(24.5836),\(72.5853)"
    let directionsAPI: String = "https://maps.googleapis.com/maps/api/directions/json?"
    let directionsUrlString: String = "\(directionsAPI)&origin=\(originString)&destination=\(destinationString)&mode=driving"

    APICall().callApiUsingWithFixURLGET(directionsUrlString, withLoader: true) { (responceData) -> Void in
        let json = responceData as! NSDictionary
        let routesArray: [AnyObject] = (json["routes"] as! [AnyObject])
        var polyline: GMSPolyline? = nil
        if routesArray.count > 0 {
            let routeDict: [NSObject : AnyObject] = routesArray[0] as! [NSObject : AnyObject]
            var routeOverviewPolyline: [NSObject : AnyObject] = (routeDict["overview_polyline"] as! [NSObject : AnyObject])
            let points: String = (routeOverviewPolyline["points"] as! String)
            let path: GMSPath = GMSPath(fromEncodedPath: points)!
            polyline = GMSPolyline(path: path)
            polyline!.strokeWidth = 2.0
            polyline!.map = self.mapView
        }

    }
}
于 2016-06-03T09:08:48.297 回答