近期做得一个基于Google maps的定位查找功能,由于国内对Google地图使用极少,所以走了不少弯路。千难万难下总算实现了功能,特地在这儿上传下代码,希望能对有需要的朋友们提供点便利。
下面的代码主要实现了这样几个功能:
1. 根据服务端经纬度定位,放置marker(单个或多个大头针)
2. 拖动地图自后根据视野中地图中心经纬度请求服务器获取该点周围的店铺
3. 点击大头针上的信息窗口之后的规划路线与导航
4. 以及一些效果实现,体验优化
上代码!———-
#import "ShopMapController.h"
@import GoogleMaps;
#import "ShopModel.h"
#import "ShopDetailController.h"
#import <XWKit/XWLocationManager.h>
@interface ShopMapController ()<GMSMapViewDelegate>
@property (strong, nonatomic) IBOutlet UIButton *refreshBtn;
@property (strong, nonatomic) IBOutlet UIButton *myLocationbtn;
@property (strong, nonatomic) IBOutlet UIImageView *centerFlagImage;
@property (strong, nonatomic) NSArray *shopList;//周边店铺列表
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *centertImageConstraints;
@end
@implementation ShopMapController{
UIButton *_refreshBtn;
GMSMapView *_mapView;
BOOL _isUserMove;
CLLocationCoordinate2D _lastCoord;
}
- (void)viewDidLoad {
[super viewDidLoad];
_isUserMove = NO;
_mapView.settings.zoomGestures = YES;
if (self.isFromDetail) {
self.title = [NSString stringWithFormat:@"%@ %@",self.shopName?LS(self.shopName):LS(@"Shop"),LS(@"Address")];
[self updateMapByShopAddress];
}else{
[self getMyLocation];
[self updateMapViewWhenNoData];
[self getNearbyShopWith:self.coordinate];
self.title = LS(@"Shop Nearby");
}
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.view removeFromSuperview];
}
//获取附近店铺
- (void)getNearbyShopWith:(CLLocationCoordinate2D)camCoord{
__weak typeof(self) ws = self;
[[HttpManager sharedInstance] fetchNearbyShopListWith:camCoord.latitude lon:camCoord.longitude token:@"" CompleteBlock:^(id response, OCResult *result) {
if (result.success && [response isKindOfClass:[NSArray class]]) {
self.shopList = (NSArray *)response;
if (self.shopList && self.shopList.count > 0) {
[_mapView clear];