ios - 在 IOS Objective-C 应用程序上向 Google map 添加标记图像

标签 ios xcode google-maps cllocation map-directions

我有一张 map 来显示方向。我可以点击一个方向来查看带有说明的注释,但我想要做的是在方向坐标上添加一个标记。这是我目前正在使用的代码:

-(void) getDirectionsFromStart: (CLLocation*) start toEnd: (CLLocation*) end
{

NSString *mapDir = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false", start.coordinate.latitude, start.coordinate.longitude, end.coordinate.latitude, end.coordinate.longitude];
if ([self.directionsArray count] == 0) {

//parse the string response and then draw the points on a map
    NSError *error;
    NSString *response = [NSString stringWithContentsOfURL:[NSURL URLWithString:mapDir] encoding:NSUTF8StringEncoding error:&error];
    if (response == nil) {
        NSLog(@"Google Maps error: %@", [error localizedDescription]);
        NSLog(@"Google Maps recovery: %@", [error localizedRecoverySuggestion]);
        //[self getDirectionsFromStart:nil toEnd:nil];
    }
    self.directionsArray = [gMapsJsonDirectionsParser parseJsonToMapDirections:response];
}
for (gMapsJourneyLeg *leg in self.directionsArray) {
    @autoreleasepool {

    self.directionsHeader.text = [NSString stringWithFormat:@"To %@ Taking: %@", leg.end_address, leg.duration.text];
    for (gMapsStep *step in leg.steps ) {
        MKPolyline *polyLine = [gMapsJsonDirectionsParser polylineWithEncodedString:step.polyLine];
        MKPolylineView *line = [[MKPolylineView alloc] initWithPolyline: polyLine];

        line.fillColor = [UIColor blueColor];
        line.strokeColor = [UIColor blueColor];
        line.lineWidth = 5;
        [self.mapView addOverlay:polyLine];
        //[self.mapView setVisibleMapRect:polyLine.boundingMapRect];
        //map point code
        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = step.end_location.coordinate;
        point.title = step.htmlInstructions;

        [[self.mapView viewForAnnotation:point] setTag:1];
        UIImage *image = [UIImage imageNamed:@"marker_red.png"];
        [[self.mapView viewForAnnotation:point] setImage:image];

        [self.mapView addAnnotation:point];
    }
    }
}

[self.tableView reloadData];
}

需要明确的是,注释和折线都很好,只是从未显示过标记。

最佳答案

基于 Google Docs for Markers 中的代码:

这是使用自定义图像添加标记的示例代码。

CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
GMSMarker *london = [GMSMarker markerWithPosition:position];
london.title = @"London";
london.icon = [UIImage imageNamed:@"house"];
london.map = mapView_;

可能是你没有设置GMSMarker *london = [GMSMarker markerWithPosition:position];这就是为什么您的标记不可见的原因。
请检查没有自定义图像的标记是否会显示,如果没有,请尝试添加 markerPosition属性(property)。

关于ios - 在 IOS Objective-C 应用程序上向 Google map 添加标记图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37297344/

相关文章:

ios - xcodebuild 相当于 Xcode 的 "Product > Build For > Testing"

javascript - Google map 上的多个标记

python - 如何使用 google-maps-services-python 按地点 id 反转地理编码位置

ios - 在 UINavigationController 中首次加载 View Controller 的事件?

ios - 如何找到 contentView 的 x 坐标等于 UIScrollView 框架的中心 x

ios - 带有空格的xcode格式数字

ios - 根据 CGFloat 的变化值自动更新 CGRect

ios - 强制 View Controller 仅纵向

ios - 想知道我的 Xcode 项目中是否存在使用 objective-c 的目录?

android - 即使没有网络也会调用 GooglePlayServicesClient onConnected 回调