百度地图无法添加标注,view forAnnotation为执行怎么办
我看网上有人说是因为delegate的问题,需要在使用annotation之前先mapview.delegate= self,但我在didload中实现了,仍无法对地图进行标注,请问怎么解决呢。
全部代码在这:大概就是我先取到当前位置,然后根据定位的坐标进行周边的poi检索,定位的时候也无法取得中心点,总而言之就是数据全部能得到,但是地图上一点动静没有。。。绝望
import UIKit
class ViewController: UIViewController, BMKMapViewDelegate, BMKPoiSearchDelegate, BMKLocationServiceDelegate {
var mapView:BMKMapView?
var search : BMKPoiSearch?
var localsever = BMKLocationService.init()
var selfLocation : CLLocationCoordinate2D?
override func viewDidLoad() {
super.viewDidLoad()
let mapView = BMKMapView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
self.view.addSubview(mapView)
mapView.delegate = self
mapView.zoomLevel = 13
mapView.isSelectedAnnotationViewFront = true
let btn = UIButton(frame: CGRect(x: 10, y: 10, width: 40, height: 40))
btn.backgroundColor = UIColor.cyan
btn.addTarget(self, action: #selector(userLocal(_:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(btn)
let searchBtn = UIButton(frame: CGRect(x: 70, y: 10, width: 60, height: 40))
searchBtn.setTitle("search", for: UIControlState())
searchBtn.backgroundColor = UIColor.cyan
searchBtn.addTarget(self, action: #selector(searchPoi(_:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(searchBtn)
}
func searchPoi(_ sender: UIButton){
search = BMKPoiSearch.init()
search?.delegate = self
var option = BMKNearbySearchOption.init()
option.pageIndex = 1
option.pageCapacity = 10
option.location = selfLocation!
option.keyword = "购物"
option.radius = 3000
var flag = search?.poiSearchNear(by: option)
if flag!{
print("检索发起成功")
}else{
print("检索发起失败")
}
}
func onGetPoiResult(_ searcher: BMKPoiSearch!, result poiResult: BMKPoiResult!, errorCode: BMKSearchErrorCode) {
mapView?.removeAnnotations(mapView?.annotations)
if errorCode == BMK_SEARCH_NO_ERROR{
var pointAnnotation = BMKPointAnnotation()
var Annotation = [BMKPointAnnotation]()
for i in 0..<poiResult.poiInfoList.count{
var poi = poiResult.poiInfoList[i] as! BMKPoiInfo
pointAnnotation.coordinate = poi.pt
pointAnnotation.title = poi.name
pointAnnotation.subtitle = poi.address
Annotation.append(pointAnnotation)
print("添加地点---\(poi.pt)---\(poi.name)----\(poi.address)")
}
self.mapView?.addAnnotations(Annotation)
self.mapView?.showAnnotations(Annotation, animated: true)
}
}
func userLocal(_ sender: UIButton){
localsever.delegate = self
mapView?.zoomLevel = 13
mapView?.showsUserLocation = true
mapView?.userTrackingMode = BMKUserTrackingModeNone
localsever.startUserLocationService()
print("定位开始")
}
func didUpdate(_ userLocation: BMKUserLocation!) {
//mapView?.centerCoordinate = userLocation.location.coordinate
mapView?.setCenter(userLocation.location.coordinate, animated: true)
selfLocation = userLocation.location.coordinate
print("定位完成,此时位于\(String(describing: selfLocation))")
localsever.stopUserLocationService()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
mapView?.viewWillAppear()
mapView?.delegate = self
search?.delegate = self
localsever.delegate = self
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
mapView?.viewWillDisappear()
mapView?.delegate = nil
search?.delegate = nil
localsever.delegate = nil
}
func mapView(_ mapView: BMKMapView!, viewFor annotation: BMKAnnotation!) -> BMKAnnotationView! {
print("start a new annotation view")
let AnnotationViewID = "renameMark"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: AnnotationViewID) as! BMKPinAnnotationView?
if annotationView == nil {
annotationView = BMKPinAnnotationView(annotation: annotation, reuseIdentifier: AnnotationViewID)
// 设置颜色
annotationView!.pinColor = UInt(BMKPinAnnotationColorRed)
// 从天上掉下的动画
annotationView!.animatesDrop = true
// 设置是否可以拖拽
annotationView!.isDraggable = false
annotationView?.canShowCallout = true
}
annotationView?.annotation = annotation
print("new annotation is complete")
return annotationView
}
}