ios - 谷歌地图上未显示注释或标记

标签 ios objective-c json google-maps annotations

有两个文件 .h 和 .m 有完整的代码,请帮我找出问题,为什么标记没有出现。 我也安装了 pod 文件和 googlemap sdk。一切正常,但只是标记没有显示,我也在日志中打印了值,它们运行良好,但 map 上没有标记或注释。

.h文件

#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>


@interface ViewController : UIViewController<GMSMapViewDelegate>{
NSArray *addressArray;
NSArray *name;
__weak IBOutlet UIView *loaderView;

}
@property (strong, nonatomic) IBOutlet GMSMapView *mapView;
@property (strong, nonatomic) ViewController *calloutView;
@property (strong, nonatomic) UIView *emptyCalloutView;

@end

.m文件

#import "ViewController.h"
#import "SimplestGoogleMapApp-Prefix.pch"
#import <GoogleMaps/GoogleMaps.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.myLocationEnabled=YES;
self.mapView.mapType=kGMSTypeNormal;
self.mapView.settings.compassButton=YES;
self.mapView.settings.myLocationButton=YES;
self.mapView.delegate=self;
[self addMarlerToMap];

}
-(void)addMarlerToMap{
NSURL *kAPIURL = [NSURL URLWithString:@"abcURl"];

NSString *jsonString = @"data={some data}";

NSData *JSONData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:kAPIURL];
request.HTTPMethod = @"POST";
request.HTTPBody = JSONData;

NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request
                                                             completionHandler:^(NSData *data,
                                                                                     NSURLResponse *response,
                                                                                 NSError *error)
                              {
                                  if (!error)
                                  {
                                      NSError *JSONError = nil;

                                      NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data
                                                                                                 options:0
                                                                                                   error:&JSONError];
                                      NSLog(@"%@",dictionary);
                                      if (JSONError)
                                      {
                                          NSLog(@"Serialization error: %@", JSONError.localizedDescription);
                                      }
                                      else
                                      {

                                          dispatch_async(dispatch_get_main_queue(), ^{
                                addressArray = dictionary[@"data"][@"centers"];


                loaderView.hidden = true;
      UIImage *pinImage = [UIImage imageNamed:@"map_gym"];

        for(int i=0;i<=[addressArray count];i++){
        self.view = _mapView;
        NSString *lat = [[addressArray objectAtIndex:i] objectForKey:@"lat"];
        NSString *lon = [[addressArray objectAtIndex:i] objectForKey:@"lng"];
              double lt=[lat doubleValue];
        double ln=[lon doubleValue];
        NSString *name = [[addressArray objectAtIndex:i] objectForKey:@"category_name"];
       NSLog(@"%@ and %@ and %f and %f of %@",lat,lon, lt,ln,name);
                                                  GMSMarker *marker = [[GMSMarker alloc] init];
                                                 // marker.animated=YES;
          marker.position = CLLocationCoordinate2DMake(lt,ln);
          marker.title = name;
          marker.map = _mapView;

     }});}}
   else
     {
      NSLog(@"Error: %@", error.localizedDescription);
                                  }
                              }];
[task resume];
 }

最佳答案

From the chat it was figured out that your map was initialised to a different location rather then the place where you were adding the markers. To solve it use this code to animate the map to the marker position

GMSCameraPosition *newCameraPosition = [GMSCameraPosition cameraWithTarget:cordinate zoom:10];
[self.mapView animateToCameraPosition:newCameraPosition];

关于ios - 谷歌地图上未显示注释或标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37019674/

相关文章:

objective-c - Xcode 项目存储在 iCloud Drive 中时出现问题

android - json 没有到达远程服务器

ios - 在 Alamofire 4 中使用 SwiftyJson 为 MultipartFormData 请求创建 JSON 数据

ios - 我可以直接在对象数据模型中创建 realm.writes 吗?

ios - 如何在具有方法的 SOAP Web 服务中传递参数?

ios - UILabel 和 NSLayoutConstraint 的垂直列表

ios - 摆脱 UINavigationBar 右侧的空间

jquery - 如何使用 json 和 jquery 显示 var?

java - 在 Android 应用程序的标签中解析 HTML 中的 JSON

ios - 从 UIView 蒙版中剪切 UIImage