地图相关
import UIKit
import MapKit
class GKMapView: UIView {
var mapView : MKMapView?
override init(frame: CGRect) {
super. init(frame: frame)
self. backgroundColor = UIColor. colr(withHexStr: "#FFFFFF")
self.initUI()
}
required init? (coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//longitude经度 latitude纬度
func showCoordinatToMap(longitude :Double, latitude :Double, schoolName:String) {
//设置区域的经纬度坐标
let nCennter = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
//设置经纬度范围(越小越精确)
let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
let region = MKCoordinateRegion(center: nCennter, span: span)
// mapView?. setRegion(region, animated: true)
mapView?. setRegion(region, animated: false)
let ann = GKAnnnotation()
ann. coordinate = nCennter
ann. title = schoolName
ann. subtitle = "学校位置"
mapView?.addAnnotation(ann)
}
func initUI() {
//设置地图的属性
mapView = MKMapView(frame: CGRect. init(x: 0, y: 0, width: zScale(x: 315), height: 123))
mapView?. mapType = .standard
mapView?.delegate = self
self.addSubview(mapView!)
mapView?.snp. makeConstraints { make in
make.edges.equalToSuperview()
}
}
}
extension GKMapView :MKMapViewDelegate {
//地图开始加载
func mapViewWillStartLoadingMap(_ mapView: MKMapView) {
}
//地图加载完毕
func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
}
//地图区域将要改变
func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
}
//地图区域已经改变
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
}
}
class GKAnnnotation: NSObject, MKAnnotation {
// Center latitude and longitude of the annotation view.
// The implementation of this property must be KVO compliant.
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
// Title and subtitle for use by selection UI.
var title: String?
var subtitle: String?
}
如果我们要搜索周围100米餐厅代码如下:
MKCoordinateRegionregion= MKCoordinateRegionMakeWithDistance(coordinate,100,100);
MKLocalSearchRequest *request= [[MKLocalSearchRequestalloc]init];
request.region = region;
request.naturalLanguageQuery = @"Restaurants";
MKLocalSearch*localSearch = [[MKLocalSearchalloc]initWithRequest:request];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response,NSError*error){
if(!error) {
//do something.
}else{
//do something.
}
}];
其中naturalLanguageQuery就是要搜索的关键字,我试过的所有关键字有cafe, supermarket,village,Community,Shop,Restaurant,School,hospital,Company,Street,Convenience store,Shopping Centre,Place names,Hotel,Grocery store每个关键字搜索返回结果只有10条,如果当前范围无搜索结果,则扩散搜索范围。如果你想列出周围所有相关位置信息,我认为需要尽可能的把所有的能够想到的关键字都举例出来进行搜索,搜索完成后进行经纬度比较然后刷选出范围内的相关位置。而且由于数据来源问题,很多位置信息都没有!当然如果你只兼容国内,还是使用百度或者腾讯地图算了。