簡體   English   中英

如何使用Google Maps ios SDK跟蹤用戶的位置並顯示行進路徑

[英]How to track a user's location and display the path travelled using Google Maps ios SDK

我目前正在構建一個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;
    }
}

這在實踐中不會在地圖上產生任何折線,所以任何幫助/建議將非常感謝!

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

如果要繪制兩條路徑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
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM