地图实现地点查找和导航

环境:系统版本:

OSX 10.10.2

Xcodel版本:7.1.1

功能:用自带地图实现查找,导航


1.首先需要在info.plist中添加NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription两个属性


导入CoreLocation.framework框架


2.创建CLLocationManager类,启动定位功能

// 定位管理
    self.locationManager = [[CLLocationManager alloc]init];
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位服务当前未打开");
        return;
    }
    // 请求用户授权
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        [self.locationManager requestWhenInUseAuthorization];
    }else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse){
        // 设置代理
        self.locationManager.delegate = self;
        // 设置定位精度
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 定位频率,每隔多少米定位一次
        CLLocationDistance distance = 10.0; // 十米定位一次
        self.locationManager.distanceFilter = distance;
        // 启动跟踪定位
        [self.locationManager startUpdatingLocation];
    }

遵守CLLocationManagerDelegate代理,实现跟踪定位

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: 'Heiti SC Light';"><span style="line-height: normal; font-family: Menlo;">// </span>跟踪定位代理</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    CLLocation *location = [locations firstObject]; // <span style="line-height: normal; font-family: 'Heiti SC Light';">取出第一个位置</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    CLLocationCoordinate2D coordinate = location.coordinate; // <span style="line-height: normal; font-family: 'Heiti SC Light';">位置坐标</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">     NSLog(@"<span style="line-height: normal; font-family: 'Heiti SC Light';">经度:</span>%f,<span style="line-height: normal; font-family: 'Heiti SC Light';">纬度:</span>%f,<span style="line-height: normal; font-family: 'Heiti SC Light';">海拔:</span>%f,<span style="line-height: normal; font-family: 'Heiti SC Light';">航向:</span>%f,<span style="line-height: normal; font-family: 'Heiti SC Light';">行走速度:</span>%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    _jinddu = coordinate.longitude;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    _weidu = coordinate.latitude;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: 'Heiti SC Light';"><span style="line-height: normal; font-family: Menlo;">    </span><span style="line-height: normal; font-family: Menlo;">//</span>如果不需要实时定位,使用完即使关闭定位服务</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    [_locationManager stopUpdatingLocation];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">}</p>

2.根据坐标取得地名

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: 'Heiti SC Light';"><span style="line-height: normal; font-family: Menlo;">// </span>根据坐标取得地名</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    //<span style="line-height: normal; font-family: 'Heiti SC Light';">反地理编码</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        CLPlacemark *placemark=[placemarks firstObject];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        NSLog(@"<span style="line-height: normal; font-family: 'Heiti SC Light';">详细信息</span>:%@",placemark.addressDictionary);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    }];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">}</p>

3.根据地名确定地理坐标

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">-(void)getCoordinateByAddress:(NSString *)address{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    //<span style="line-height: normal; font-family: 'Heiti SC Light';">地理编码</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    [_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: 'Heiti SC Light';"><span style="line-height: normal; font-family: Menlo;">        </span><span style="line-height: normal; font-family: Menlo;">//</span>取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        CLPlacemark *placemark=[placemarks firstObject];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;">        </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        CLLocation *location=placemark.location;//<span style="line-height: normal; font-family: 'Heiti SC Light';">位置</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        CLRegion *region=placemark.region;//<span style="line-height: normal; font-family: 'Heiti SC Light';">区域</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        NSDictionary *addressDic= placemark.addressDictionary;//<span style="line-height: normal; font-family: 'Heiti SC Light';">详细地址信息字典</span>,<span style="line-height: normal; font-family: 'Heiti SC Light';">包含以下部分信息</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *name=placemark.name;//<span style="line-height: normal; font-family: 'Heiti SC Light';">地名</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *thoroughfare=placemark.thoroughfare;//<span style="line-height: normal; font-family: 'Heiti SC Light';">街道</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *subThoroughfare=placemark.subThoroughfare; //<span style="line-height: normal; font-family: 'Heiti SC Light';">街道相关信息,例如门牌等</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *locality=placemark.locality; // <span style="line-height: normal; font-family: 'Heiti SC Light';">城市</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *subLocality=placemark.subLocality; // <span style="line-height: normal; font-family: 'Heiti SC Light';">城市相关信息,例如标志性建筑</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *administrativeArea=placemark.administrativeArea; // <span style="line-height: normal; font-family: 'Heiti SC Light';">州</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *subAdministrativeArea=placemark.subAdministrativeArea; //<span style="line-height: normal; font-family: 'Heiti SC Light';">其他行政区域信息</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *postalCode=placemark.postalCode; //<span style="line-height: normal; font-family: 'Heiti SC Light';">邮编</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *ISOcountryCode=placemark.ISOcountryCode; //<span style="line-height: normal; font-family: 'Heiti SC Light';">国家编码</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *country=placemark.country; //<span style="line-height: normal; font-family: 'Heiti SC Light';">国家</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *inlandWater=placemark.inlandWater; //<span style="line-height: normal; font-family: 'Heiti SC Light';">水源、湖泊</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSString *ocean=placemark.ocean; // <span style="line-height: normal; font-family: 'Heiti SC Light';">海洋</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        //        NSArray *areasOfInterest=placemark.areasOfInterest; //<span style="line-height: normal; font-family: 'Heiti SC Light';">关联的或利益相关的地标</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">        NSLog(@"<span style="line-height: normal; font-family: 'Heiti SC Light';">位置</span>:%@,<span style="line-height: normal; font-family: 'Heiti SC Light';">区域</span>:%@,<span style="line-height: normal; font-family: 'Heiti SC Light';">详细信息</span>:%@",location,region,addressDic);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    }];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">}</p>
   

使用苹果自带地图

1.根据单个地名在地图上定位

-(void)location{
    //地理编码
    [_geocoder geocodeAddressString:@"上海市" completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *clPlacemark=[placemarks firstObject];//获取第一个地标
        MKPlacemark *mkplacemark=[[MKPlacemark alloc]initWithPlacemark:clPlacemark];//定位地标转化为地图的地标
        NSDictionary *options=@{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard)};
        MKMapItem *mapItem=[[MKMapItem alloc]initWithPlacemark:mkplacemark];
        [mapItem openInMapsWithLaunchOptions:options];
    }];
}

2.定位多个地名

- (void)listPlacemark
{
    //根据“北京市”进行地理编码
    [_geocoder geocodeAddressString:@"北京市" completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *clPlacemark1=[placemarks firstObject];//获取第一个地标
        MKPlacemark *mkPlacemark1=[[MKPlacemark alloc]initWithPlacemark:clPlacemark1];
        
        //注意地理编码一次只能定位到一个位置,不能同时定位,所在放到第一个位置定位完成回调函数中再次定位
        [_geocoder geocodeAddressString:@"郑州市" completionHandler:^(NSArray *placemarks, NSError *error) {
            
            CLPlacemark *clPlacemark2=[placemarks firstObject];//获取第一个地标
            MKPlacemark *mkPlacemark2=[[MKPlacemark alloc]initWithPlacemark:clPlacemark2];
            NSDictionary *options=@{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard)};
            //MKMapItem *mapItem1=[MKMapItem mapItemForCurrentLocation];//当前位置
            MKMapItem *mapItem1=[[MKMapItem alloc]initWithPlacemark:mkPlacemark1];
            MKMapItem *mapItem2=[[MKMapItem alloc]initWithPlacemark:mkPlacemark2];
            [MKMapItem openMapsWithItems:@[mapItem1,mapItem2] launchOptions:options];
        }];
    }];
}

3.实现导航功能

-(void)turnByTurn{
    //根据“北京市”地理编码
    [_geocoder geocodeAddressString:@"北京市" completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *clPlacemark1=[placemarks firstObject];//获取第一个地标
        MKPlacemark *mkPlacemark1=[[MKPlacemark alloc]initWithPlacemark:clPlacemark1];
        //注意地理编码一次只能定位到一个位置,不能同时定位,所在放到第一个位置定位完成回调函数中再次定位
        [_geocoder geocodeAddressString:@"郑州市" completionHandler:^(NSArray *placemarks, NSError *error) {
            CLPlacemark *clPlacemark2=[placemarks firstObject];//获取第一个地标
            MKPlacemark *mkPlacemark2=[[MKPlacemark alloc]initWithPlacemark:clPlacemark2];
            NSDictionary *options=@{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard),MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving};
            //MKMapItem *mapItem1=[MKMapItem mapItemForCurrentLocation];//当前位置
            MKMapItem *mapItem1=[[MKMapItem alloc]initWithPlacemark:mkPlacemark1];
            MKMapItem *mapItem2=[[MKMapItem alloc]initWithPlacemark:mkPlacemark2];
            [MKMapItem openMapsWithItems:@[mapItem1,mapItem2] launchOptions:options];
        }];
    }];
}







<think>好的,我现在需要分析用户提供的这段JavaScript代码,并解释它的功能。首先,我会通读代码,理解每个部分的作用。 这段代码定义了一个名为getQ的函数。函数一开始获取了user_nickname输入框的值,存储在userid变量中。如果userid是null或undefined,函数返回false。否则,发起一个AJAX POST请求到../services/Qxgl.ashx,传递actionid参数。请求头里有一个Authorization令牌,看起来像是某种认证信息。 成功回调函数处理返回的msg数据,遍历msg.ds数组,对每个item,通过typeid设置对应的复选框为选中状态。然后收集所有选中的复选框的值,用逗号拼接后去掉最后一个逗号,将结果填入typename输入框。 接下来,我需要确认各个部分的细节是否正确。例如,检查jQuery选择器是否正确,AJAX调用的参数是否合理,以及数据处理是否有潜在错误。比如,$.each遍历msg.ds是否正确,假设msg.ds是一个数组。另外,拼接结果时使用substring去除末尾逗号是否可靠,是否有更稳妥的方法。 还要注意可能的安全问题,比如硬编码的Authorization头,这可能存在安全风险,但用户可能已经处理了。此外,用户权限验证是否在服务端进行,这里只是前端控制,需提醒用户不能仅依赖前端验证。 最后,将这些分析整理成结构清晰的解释,分步骤说明代码的功能潜在注意事项,确保用户能够理解每个部分的作用及可能的改进点。</think>这是一个典型的基于jQuery的AJAX请求实现,主要用于获取并设置用户权限信息。让我们分步骤解析这个函数的功能: 1. **函数定义与用户ID获取** ```javascript var getQ = function () { var userid = $('#user_nickname').val(); // 获取用户输入框的值 ``` 2. **空值校验** ```javascript if (userid == null || userid == undefined) { // 验证用户ID有效性 return false; } ``` 3. **AJAX请求配置** ```javascript $.ajax({ type: "POST", // 使用POST方法 url: "../services/Qxgl.ashx", // 后端接口地址 headers: { // 请求头认证信息 "Authorization": "0772A4AA051DDEBB393DC164B19A32E57C4FEEAB27A77820" }, ``` 4. **请求参数传递** ```javascript data: { action: "getQ", // 执行动作类型 id: userid // 传递用户ID参数 }, ``` 5. **成功回调处理** ```javascript success: function (msg) { // 遍历返回数据集 $.each(msg.ds, function (index, item) { // 根据typeid勾选对应复选框 $('#a' + item.typeid).attr("checked", true); }) // 拼接选中值 var result = ""; $("input[name='select_checkbox']:checked").each(function () { result += $(this).val() + ','; }); // 去除末尾逗号 if (result != "") { result = result.substring(0, result.lastIndexOf(',')); } // 填充到显示框 $("#typename").val(result); } ``` **功能总结**: - 这是一个用户权限查询功能 - 通过用户输入的用户名查询关联的权限配置 - 将查询结果转换为复选框勾选状态 - 汇总选中权限显示在文本框中 **潜在注意事项**: 1. 硬编码的`Authorization`头存在安全风险,建议动态生成 2. 缺少错误处理逻辑(error回调) 3. 使用字符串拼接处理结果可以考虑改用`Array.join()` 4. 权限控制不能仅依赖前端验证,必须配套后端验证 5. 对`msg.ds`的数据结构有强依赖,建议增加数据校验 6. 建议使用`.prop()`代替`.attr()`操作复选框状态
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值