如何使用Google Maps ios SDK跟踪用户的位置并显示行进路径

时间:2013-08-24 20:29:20

标签: ios google-maps google-maps-sdk-ios polyline

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

到目前为止,我已初始化Google地图视图,并可使用 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;
    }
}

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

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:2)

如果要绘制两条路径BTW两个地方使用此方法。它的演示swift方法可以找到两个地方的溃败。

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

    }
}