|
|
|
//
|
|
|
|
// GoogleMapView.m
|
|
|
|
// LekangGuard
|
|
|
|
//
|
|
|
|
// Created by ecell on 2022/11/18.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "GoogleMapView.h"
|
|
|
|
|
|
|
|
@interface GoogleMapView ()<GMSMapViewDelegate>
|
|
|
|
|
|
|
|
@property (nonatomic,strong) GMSMapView *GmapView;//地图
|
|
|
|
@property (nonatomic,strong) GMSMarker *marker;//大头针
|
|
|
|
@property (nonatomic,assign) CLLocationCoordinate2D coordinate2D;
|
|
|
|
@property (nonatomic,strong) GMSPlacesClient * placesClient;//可以获取某个地方的信息
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GoogleMapView
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
self = [super initWithFrame:frame];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
self.backgroundColor = KKClearColor;
|
|
|
|
[self subViews];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setLaloModel:(LatestLocationModel *)laloModel
|
|
|
|
{
|
|
|
|
_laloModel = laloModel;
|
|
|
|
|
|
|
|
CLLocationCoordinate2D d2 = CLLocationCoordinate2DMake(laloModel.latitude.doubleValue, laloModel.longitude.doubleValue);
|
|
|
|
_coordinate2D = d2;///[JZLocationConverter wgs84ToGcj02:d2];
|
|
|
|
_GmapView.camera = [GMSCameraPosition cameraWithTarget:_coordinate2D zoom:15];
|
|
|
|
[self mapView:self.GmapView idleAtCameraPosition:_GmapView.camera];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)subViews
|
|
|
|
{
|
|
|
|
self.laloModel = APIManager.sharedManager.laloModel;
|
|
|
|
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:self.laloModel.latitude.doubleValue
|
|
|
|
longitude:self.laloModel.longitude.doubleValue
|
|
|
|
zoom:15];
|
|
|
|
self.GmapView = [GMSMapView mapWithFrame:self.bounds camera:camera];
|
|
|
|
self.GmapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
|
[self.GmapView setMapType:kGMSTypeNormal];//地图类型
|
|
|
|
//设置缩放级别限制
|
|
|
|
// [mapView setMinZoom:10 maxZoom:15];
|
|
|
|
self.GmapView.myLocationEnabled = YES;//我的定位poi
|
|
|
|
self.GmapView.indoorEnabled = NO;//室内地图.
|
|
|
|
self.GmapView.delegate = self;
|
|
|
|
[self addSubview:self.GmapView];
|
|
|
|
|
|
|
|
self.marker = [[GMSMarker alloc] init];
|
|
|
|
self.marker.tracksViewChanges = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma 缩小地图
|
|
|
|
- (void)setMapZoom:(CLLocationCoordinate2D)centerCoordinate
|
|
|
|
{
|
|
|
|
@try {
|
|
|
|
if (CLLocationCoordinate2DIsValid(centerCoordinate))
|
|
|
|
{
|
|
|
|
_coordinate2D = centerCoordinate;/// [JZLocationConverter wgs84ToGcj02:centerCoordinate];
|
|
|
|
_GmapView.camera = [GMSCameraPosition cameraWithTarget:_coordinate2D zoom:15];
|
|
|
|
[self mapView:self.GmapView idleAtCameraPosition:_GmapView.camera];
|
|
|
|
}
|
|
|
|
} @catch (NSException *exception) {
|
|
|
|
|
|
|
|
} @finally {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
|
|
|
|
{
|
|
|
|
if (response.regeocode != nil)
|
|
|
|
{
|
|
|
|
NSString *name = response.regeocode.formattedAddress;
|
|
|
|
//解析response获取地址描述,具体解析见 Demo
|
|
|
|
//获取市
|
|
|
|
// NSString *city = response.regeocode.addressComponent.city;
|
|
|
|
// NSString *province = response.regeocode.addressComponent.province;
|
|
|
|
// if(city && city.length>0){
|
|
|
|
// name = [name componentsSeparatedByString:city].lastObject;
|
|
|
|
// if([city isEqualToString:province]){
|
|
|
|
// //直辖市
|
|
|
|
// name = [NSString stringWithFormat:@"%@ %@",name,city];
|
|
|
|
// }else{
|
|
|
|
// //普通市
|
|
|
|
// name = [NSString stringWithFormat:@"%@ %@ %@",name,city,province];
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
//self.locationAddressLabel.text = name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)mapViewDidFinishTileRendering:(GMSMapView *)mapView
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
//地图移动后的代理方法,我这里的需求是地图移动需要刷新网络请求,
|
|
|
|
- (void)mapView:(GMSMapView*)mapView idleAtCameraPosition:(GMSCameraPosition*)position
|
|
|
|
{
|
|
|
|
// //点击一次先清除上一次的大头针
|
|
|
|
[self.marker.map clear];
|
|
|
|
self.marker.map = nil;
|
|
|
|
if (self.laloModel)
|
|
|
|
{
|
|
|
|
self.marker = [GMSMarker markerWithPosition:_coordinate2D];
|
|
|
|
self.marker.iconView = [self markView];
|
|
|
|
self.marker.map = self.GmapView;
|
|
|
|
|
|
|
|
[self getPlace:mapView.camera.target];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIView *)markView
|
|
|
|
{
|
|
|
|
UIView *view = [UIView new];
|
|
|
|
view.frame = CGRectMake(0, 0, 41, 60);
|
|
|
|
|
|
|
|
UIImageView *bgImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"宝贝-无头像"]];
|
|
|
|
bgImg.contentMode = UIViewContentModeScaleAspectFit;
|
|
|
|
bgImg.frame = CGRectMake(0, 3, 41, 57);
|
|
|
|
[view addSubview:bgImg];
|
|
|
|
|
|
|
|
UIImageView *iconImg = [[UIImageView alloc] init];
|
|
|
|
NSString *imageName = @"设备默认头像";
|
|
|
|
if(APIManager.sharedManager.deviceModel.sex == 0)
|
|
|
|
{
|
|
|
|
//女
|
|
|
|
imageName = @"icon_head_girl";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//男
|
|
|
|
imageName = @"icon_head_boy";
|
|
|
|
}
|
|
|
|
[iconImg sd_setImageWithURL:[NSURL URLWithString:APIManager.sharedManager.deviceModel.image] placeholderImage:[UIImage imageNamed:imageName]];
|
|
|
|
iconImg.frame = CGRectMake(7, 15, 27, 27);
|
|
|
|
[view addSubview:iconImg];
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)getPlace:(CLLocationCoordinate2D)coordinate2D
|
|
|
|
{
|
|
|
|
[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(coordinate2D.latitude, coordinate2D.longitude) completionHandler:^(GMSReverseGeocodeResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
|
if(!error){
|
|
|
|
GMSAddress* addressObj = response.firstResult;
|
|
|
|
NSLog(@"coordinate.latitude=%f", addressObj.coordinate.latitude);
|
|
|
|
NSLog(@"coordinate.longitude=%f", addressObj.coordinate.longitude);
|
|
|
|
NSLog(@"thoroughfare=%@", addressObj.thoroughfare);
|
|
|
|
NSLog(@"locality=%@", addressObj.locality);
|
|
|
|
NSLog(@"subLocality=%@", addressObj.subLocality);
|
|
|
|
NSLog(@"administrativeArea=%@", addressObj.administrativeArea);
|
|
|
|
NSLog(@"postalCode=%@", addressObj.postalCode);
|
|
|
|
NSLog(@"country=%@", addressObj.country);
|
|
|
|
NSLog(@"lines=%@", addressObj.lines);
|
|
|
|
|
|
|
|
// NSString *name = addressObj.administrativeArea;
|
|
|
|
// //解析response获取地址描述,具体解析见 Demo
|
|
|
|
// //获取市
|
|
|
|
// NSString *city = addressObj.locality;
|
|
|
|
// NSString *province = addressObj.subLocality;
|
|
|
|
// NSString *lines = addressObj.lines.count > 0 ? addressObj.lines[0] : @"";
|
|
|
|
// if(city && city.length>0)
|
|
|
|
// {
|
|
|
|
// name = [name componentsSeparatedByString:city].lastObject;
|
|
|
|
// if([city isEqualToString:province]){
|
|
|
|
// //直辖市
|
|
|
|
// name = [NSString stringWithFormat:@"%@ %@",name,city];
|
|
|
|
// }else{
|
|
|
|
// //普通市
|
|
|
|
// name = [NSString stringWithFormat:@"%@ %@ %@ %@",name,city,province,lines];
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// self.locationAddressLabel.text = addressObj.lines.count > 0 ? addressObj.lines[0] : @"";
|
|
|
|
|
|
|
|
}else{
|
|
|
|
NSLog(@"地理反编码错误");
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
//选择了位置后的回调方法
|
|
|
|
- (void)viewController:(GMSAutocompleteViewController*)viewController didAutocompleteWithPlace:(GMSPlace*)place
|
|
|
|
{
|
|
|
|
//移动地图中心到选择的位置
|
|
|
|
_GmapView.camera = [GMSCameraPosition cameraWithTarget:place.coordinate zoom:15];
|
|
|
|
[viewController dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
//失败回调
|
|
|
|
- (void)viewController:(GMSAutocompleteViewController *)viewController
|
|
|
|
didFailAutocompleteWithError:(NSError *)error
|
|
|
|
{
|
|
|
|
[viewController dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
//取消回调
|
|
|
|
- (void)wasCancelled:(GMSAutocompleteViewController *)viewController
|
|
|
|
{
|
|
|
|
[viewController dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
// Only override drawRect: if you perform custom drawing.
|
|
|
|
// An empty implementation adversely affects performance during animation.
|
|
|
|
- (void)drawRect:(CGRect)rect {
|
|
|
|
// Drawing code
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
@end
|