在IOS中跟踪Google Map中的用户位置

时间:2015-07-07 07:10:49

标签: ios xcode

我在我的iOS项目中使用谷歌地图SDK来跟踪用户位置并绘制用户路径的路线以进行移动。我想为用户的起点添加两个图像,为用户移动添加另一个图像。我无法添加这些图片。请帮我添加这些图片。这是我的代码:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

//get the latest location
CLLocation *currentLocation = [locations lastObject];

//store latest location in stored track array;
[self.locations addObject:currentLocation];

//get latest location coordinates
CLLocationDegrees Latitude = currentLocation.coordinate.latitude;
CLLocationDegrees Longitude = currentLocation.coordinate.longitude;
CLLocationCoordinate2D locationCoordinates = CLLocationCoordinate2DMake(Latitude, Longitude);

//zoom map to show users location
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(locationCoordinates, 1000,1000);

MKCoordinateRegion adjustedRegion = [mapview regionThatFits:viewRegion];
[mapview setRegion:adjustedRegion animated:YES];


NSInteger numberOfSteps = self.locations.count;

CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
    CLLocation *location = [self.locations objectAtIndex:index];
    CLLocationCoordinate2D coordinate2 = location.coordinate;

    coordinates[index] = coordinate2;
}

MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[mapview addOverlay:polyLine];

}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineView *polylineView = [[MKPolylineView alloc]     initWithPolyline:overlay];
    polylineView.strokeColor = [UIColor redColor];
    polylineView.lineWidth = 10.0;
    return polylineView;
}

此代码绘制用户路径。但我想要这种output。 有人请帮帮我。

2 个答案:

答案 0 :(得分:0)

为map.locations数组的第一个和最后一个对象添加注释到mapview。每次将位置更新为新注释时,您还需要删除旧注释。

答案 1 :(得分:0)

让centerY和centerX成为图像的中心点

CLLocationDegrees offset = 0.01;  // change size here
  CLLocationDegrees southWestY = centerY - offset;
  CLLocationDegrees southWestX = centerX - offset;
  CLLocationDegrees northEastY = centerY + offset;
  CLLocationDegrees northEastX = centerX + offset;

  CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(southWestY,southWestX);
  CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(northEastY,northEastX);
  GMSCoordinateBounds *overlayBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWest coordinate:northEast];

  GMSGroundOverlay *overlay = [GMSGroundOverlay groundOverlayWithBounds:overlayBounds icon:[UIImage imagedNamed:@"YourImage.png"]];
  overlay.bearing = 0;
  overlay.map = self.mapView

使用这种方法(与标记不同),当用户放大/缩小地图时,图像会放大和缩小。