如何跟踪用户的位置并显示使用Google Maps ios SDK的路径 [英] How to track a user's location and display the path travelled using Google Maps ios SDK

查看:95
本文介绍了如何跟踪用户的位置并显示使用Google Maps ios SDK的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



到目前为止,我已初始化Google地图视图,并可以使用 observeValueForKeyPath 显示用户的当前位置。功能。



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

   - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id )对象变化:(的NSDictionary *)变化上下文:(无效*)上下文
{
如果([的keyPath isEqualToString:@ myLocation]&安培;&安培; [对象isKindOfClass:[GMSMapView中类]])
{
[self.myMapView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:self.myMapView.myLocation.coordinate.latitude
经度:self.myMapView.myLocation.coordinate.longitude
变焦: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 *)经理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];
为(INT I = 0; I< self.points.count;我++)
{
的NSArray * latlongArray = [[self.points objectAtIndex:ⅰ] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@ ]];

[路径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_;
}}


I'm currently building an ios app and I'm hoping to implement a function where the user's location is displayed on a Google Map view and when they move a polyline show's the path that has been travelled by the user so far. This obviously needs to happen in realtime.

So far I've initialised a Google Maps view and can display the user's current location using the observeValueForKeyPath function. The view and location marker update as the user moves.

I figured the best way to do this would be to create a GMSMutablePath object that adds the user's current location every time the map camera updates to the new location and then create a polyline from that path. The code I've used for this is below:

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

This doesn't produce any polyline on the map in practice so any help/advice would be much appreciated!

解决方案

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

这篇关于如何跟踪用户的位置并显示使用Google Maps ios SDK的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆