Google 如果标记位置每秒更新一次,iOS 上的地图没有响应
Google Maps on iOS is not responsive if marker position is updated every second
我下载了最新的 Google Maps for iOS SDK,根据文档与我的项目集成。地图显示正常。我有一个标记,我在线程中每隔一秒更新一次。当标记开始在地图上更新时,地图变得非常迟钝。缩放、平移等需要几秒钟时间,给人一种地图无法正常工作的感觉。有人可以帮忙吗?
第二个问题:当我不更新 UI 时,我可以缩放、平移等。那时,我覆盖在 google 地图上的地图会调整大小或四处移动,即使我正在设置要锚定图像的坐标。我如何阻止它并保持地图固定?
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <GoogleMaps/GoogleMaps.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate, GMSIndoorDisplayDelegate, GMSMapViewDelegate>
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@property int counter;
@property (nonatomic, strong) GMSMapView *mapView;
@property (nonatomic, strong) GMSMarker *myMarker;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) CLLocationCoordinate2D location;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.295210
longitude:-124.032841
zoom:10.5];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.mapView.delegate = self;
self.view = self.mapView;
[self.mapView setMapType:kGMSTypeHybrid];
/* Location Manager stuff */
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self stopSending];
}
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self stopSending];
}
- (void) overlapMap {
self.mapView.mapType = kGMSTypeNormal;
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(40.295048, -124.033110);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(40.295385, -124.032801);
GMSGroundOverlay *overlay = [self setGroundOverlayWithSouthWestCoordinate:southWest AndNorthEastCoordinate:northEast AndImage:[UIImage imageNamed:@"floorplan.png"]];
overlay.map = self.mapView;
[self.mapView animateToCameraPosition:[GMSCameraPosition
cameraWithLatitude:40.295210
longitude:-124.032951
zoom:30]];
[self.mapView setUserInteractionEnabled:true];
[self showMarkers];
}
- (void) showMarkers {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateMarkerDetails) userInfo:nil repeats:YES];
}
- (void) updateMarkerDetails {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
CLLocationCoordinate2D location = [self getLocation];
if (CLLocationCoordinate2DIsValid(location) {
self.myMarker = [GMSMarker markerWithPosition:location];
self.myMarker.map = self.mapView;
self.osLocation.map = self.mapView;
}
[self.mapView setSelectedMarker:self.myMarker];
/* some code with the marker title and subtitle*/
dispatch_async(dispatch_get_main_queue(), ^{
[self.myMarker setPosition:location];
});
});
}
- (void) clearMap {
self.myMarker.map = nil;
self.osLocation.map = nil;
[self.timer invalidate];
self.timer = nil;
}
- (CLLocationCoordinate2D) getLocation {
return self.location;
}
/*
Location Manager related code
Some other proprietary code
*/
@end
我对问题进行了反复试验,发现当我们使用 google 地图时,所有与网络相关的操作都应该在后台线程中进行,而不是某些网络相关的操作。我没有在后台线程中执行所有与网络相关的操作。我改变了一些东西,响应能力得到了解决。
我下载了最新的 Google Maps for iOS SDK,根据文档与我的项目集成。地图显示正常。我有一个标记,我在线程中每隔一秒更新一次。当标记开始在地图上更新时,地图变得非常迟钝。缩放、平移等需要几秒钟时间,给人一种地图无法正常工作的感觉。有人可以帮忙吗?
第二个问题:当我不更新 UI 时,我可以缩放、平移等。那时,我覆盖在 google 地图上的地图会调整大小或四处移动,即使我正在设置要锚定图像的坐标。我如何阻止它并保持地图固定?
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <GoogleMaps/GoogleMaps.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate, GMSIndoorDisplayDelegate, GMSMapViewDelegate>
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@property int counter;
@property (nonatomic, strong) GMSMapView *mapView;
@property (nonatomic, strong) GMSMarker *myMarker;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) CLLocationCoordinate2D location;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.295210
longitude:-124.032841
zoom:10.5];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.mapView.delegate = self;
self.view = self.mapView;
[self.mapView setMapType:kGMSTypeHybrid];
/* Location Manager stuff */
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self stopSending];
}
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self stopSending];
}
- (void) overlapMap {
self.mapView.mapType = kGMSTypeNormal;
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(40.295048, -124.033110);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(40.295385, -124.032801);
GMSGroundOverlay *overlay = [self setGroundOverlayWithSouthWestCoordinate:southWest AndNorthEastCoordinate:northEast AndImage:[UIImage imageNamed:@"floorplan.png"]];
overlay.map = self.mapView;
[self.mapView animateToCameraPosition:[GMSCameraPosition
cameraWithLatitude:40.295210
longitude:-124.032951
zoom:30]];
[self.mapView setUserInteractionEnabled:true];
[self showMarkers];
}
- (void) showMarkers {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateMarkerDetails) userInfo:nil repeats:YES];
}
- (void) updateMarkerDetails {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
CLLocationCoordinate2D location = [self getLocation];
if (CLLocationCoordinate2DIsValid(location) {
self.myMarker = [GMSMarker markerWithPosition:location];
self.myMarker.map = self.mapView;
self.osLocation.map = self.mapView;
}
[self.mapView setSelectedMarker:self.myMarker];
/* some code with the marker title and subtitle*/
dispatch_async(dispatch_get_main_queue(), ^{
[self.myMarker setPosition:location];
});
});
}
- (void) clearMap {
self.myMarker.map = nil;
self.osLocation.map = nil;
[self.timer invalidate];
self.timer = nil;
}
- (CLLocationCoordinate2D) getLocation {
return self.location;
}
/*
Location Manager related code
Some other proprietary code
*/
@end
我对问题进行了反复试验,发现当我们使用 google 地图时,所有与网络相关的操作都应该在后台线程中进行,而不是某些网络相关的操作。我没有在后台线程中执行所有与网络相关的操作。我改变了一些东西,响应能力得到了解决。