简体   繁体   English

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

[英]In IOS track user location in Google Map

I am using google map SDKs in my iOS project to track the user location and a draw a route line of the user path for his movement.I want to add two images for the starting point of the user and another for user movement. 我在我的iOS项目中使用google map SDK来跟踪用户位置并绘制用户路径以进行移动。我想为用户的起点添加两个图像,为用户移动添加另一个图像。 I can't add these images.Please help me to add these images. 我无法添加这些图像。请帮助我添加这些图像。 Here is my code: 这是我的代码:

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

This code draw the user path. 此代码绘制用户路径。 But I want this type of output . 但是我想要这种类型的输出 Someone please help me. 有人请帮助我。

Add annotations to mapview, for first and last objects of your self.locations array. 为您的self.locations数组的第一个和最后一个对象向mapview添加注释。 You will also need to remove the old annotation each time the location is updated to a new one. 每当位置更新为新注释时,您还需要删除旧注释。

Let centerY and centerX be the center point for the image 设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

With this approach (unlike with the marker) the image will zoom in and out as the user zooms in/out of the map. 使用此方法(与标记不同),图像将随着用户放大/缩小地图而放大和缩小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM