目前,我知道如何使用静态引脚(添加的图像)创建 MKAnnotationView。
有谁知道或有任何资源,如何创建一个可以改变颜色的图钉,或者在其中显示一个数字,该数字会根据有关业务的信息而变化?
例如,我希望在营业结束时将图钉显示为红色,在营业时将图钉显示为绿色。甚至可能在图钉内有一个美元符号来告诉用户它有多贵。
编辑
我创建了一个名为CustomPin采用该MKAnnotation协议的类。此外,我不希望有一个MKAnnotationView可以更改的自定义图像。这是否意味着我必须MKAnnotationView在数组中添加多个图像,并在每次有关业务的详细信息发生变化时更改图像?
先感谢您!
我想显示两个坐标之间的地图。我已使用此代码来显示包含两个坐标的地图矩形。但我的两个坐标都没有显示,而是显示了两个坐标之间的区域(不包括坐标)。我应该怎么做才能包含我的两个坐标?
let coordinate1 = CLLocationCoordinate2DMake(28.53, 77.39)
let coordinate2 = CLLocationCoordinate2DMake(29.13,76.69)
// convert them to MKMapPoint
let p1 = MKMapPointForCoordinate (coordinate2);
let p2 = MKMapPointForCoordinate (coordinate1);
let mapRect = MKMapRectMake(fmin(p1.x,p2.x), fmin(p1.y,p2.y), fabs(p1.x-p2.x), fabs(p1.y-p2.y));
mapView.setVisibleMapRect(mapRect, animated: true)
Run Code Online (Sandbox Code Playgroud) 我正在根据方向绘制叠加层。我从路线a绘制到路线b,然后从路线b绘制到路线c。
我想检测是否在 mkoverlay 上的任何位置点击了叠加层。
我使用了这个例子Detecting Touchs on MKOverlay in iOS7 (MKOverlayRenderer)
并将其转换为 swift 。
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if touch.tapCount == 1 {
let touchLocation = touch.location(in: self)
let locationCoordinate = self.convert(touchLocation, toCoordinateFrom: self)
let mapPoint: MKMapPoint = MKMapPointForCoordinate(locationCoordinate)
let mapPointAsCGP = CGPoint(x: CGFloat(mapPoint.x), y: CGFloat(mapPoint.y))
for overlay: MKOverlay in self.overlays {
if (overlay is MKPolygon) {
let polygon: MKPolygon? = (overlay as? MKPolygon)
let mpr: CGMutablePath = …Run Code Online (Sandbox Code Playgroud) 我使用 init 方法在 Map 添加了一些注释视图(由 id 初始化)。现在我想更新导航栏中单击按钮上的特定 id 注释视图。
假设我添加了 5 个带有 ids 的注释(1、2、3、4 和 5)
从VC添加:
let annotation = MapPinAnnotation(title: storeItem.name!, location: CLLocationCoordinate2DMake(Double(lat), Double(long)), id: storeItem.storeId!)
self.mapview.addAnnotation(annotation)
Run Code Online (Sandbox Code Playgroud)
初始化的AnnotationView:
class MapPinAnnotation: NSObject, MKAnnotation {
var title:String?
var id:String?
private(set) var coordinate = CLLocationCoordinate2D()
init(title newTitle: String, location: CLLocationCoordinate2D, id: String) {
super.init()
self.title = newTitle
self.coordinate = location
self.id = id
}
}
Run Code Online (Sandbox Code Playgroud)
ViewFor注解方法:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation is MKUserLocation) {
return …Run Code Online (Sandbox Code Playgroud) 我在Apple地图中添加MKAnnotationView,并在annotationView中创建一个点击事件。但是Mylocation标记正在与annotationView点击交互。如何克服这个问题。希望您理解我的问题。提前致谢。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = MKAnnotationView()
guard let annotation = annotation as? ClientLocation
else{
return nil
}
if let dequedView = mapView.dequeueReusableAnnotationView(withIdentifier: annotation.identifier){
annotationView = dequedView
}
else{
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotation.identifier)
}
////////////////////////
///////////////////
annotationView.rightCalloutAccessoryView = button
annotationView.canShowCallout = true
return annotationView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
}
Run Code Online (Sandbox Code Playgroud)
我正在斯威夫特绘制导航道路。我正在使用当前位置到另一个位置并进行平局。之后,我选择另一个位置并重新绘制它。但即使我写 mapView.remove(rotapoly)了我的代码,它也不会删除它。我该如何解决这个问题?
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
cizim = 1;
let capital = view.annotation as! Station
guard let locValue: CLLocationCoordinate2D = locationManager.location?.coordinate else { return }
let neresi = CLLocationCoordinate2D(latitude: capital.latitude, longitude: capital.longitude)
let nerdeyim = CLLocationCoordinate2D(latitude: locValue.latitude, longitude: locValue.longitude)
let request = MKDirectionsRequest()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: nerdeyim, addressDictionary: nil))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: neresi, addressDictionary: nil))
request.requestsAlternateRoutes = true
request.transportType = .walking
let directions = MKDirections(request: request)
directions.calculate { [unowned self] …Run Code Online (Sandbox Code Playgroud) Xcode 11.1、斯威夫特 4
如何在 Swift 中使用 Mapkit 在三个位置(点)之间画一条线?
let london = Capital(title: "London", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), info: "Home to the 2012 Summer Olympics.")
let oslo = Capital(title: "Oslo", coordinate: CLLocationCoordinate2D(latitude: 59.95, longitude: 10.75), info: "Founded over a thousand years ago.")
Run Code Online (Sandbox Code Playgroud)
谢谢
如何改变MKMarkerAnnotationView尺寸?
我尝试设置annotationView.bounds.size = CGSize(width: 50, height: 50),但看起来大小没有改变。我还尝试打印视图的大小,看起来默认为 28,28
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else { return nil }
let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: Constant.Indentifier.mapPoint)
annotationView.canShowCallout = true
annotationView.animatesWhenAdded = true
annotationView.glyphImage = UIImage(systemName: "house.fill")
annotationView.glyphTintColor = .systemBlue
annotationView.markerTintColor = .white
print(annotationView.bounds.size) // defaulted to 28,28
annotationView.bounds.size = CGSize(width: 50, height: 50) // Does not change bubble size
return annotationView
}
Run Code Online (Sandbox Code Playgroud) 我的代码与问题相关:
注释.h文件
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface CityAnnotation : NSObject <MKAnnotation> {
NSString *name;
NSString *country;
CLLocationCoordinate2D coords;
}
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *country;
@property (nonatomic,assign) CLLocationCoordinate2D coords;
-(id)initWithTitle:(NSString *)cityname AndCountry:(NSString *)countryname AndCoords: (CLLocationCoordinate2D)coordinate;
@end
Run Code Online (Sandbox Code Playgroud)
注释.m文件
#import "CityAnnotation.h"
@implementation CityAnnotation
@synthesize name,country,coords;
-(id)initWithTitle:(NSString *)cityname AndCountry:(NSString *)countryname AndCoords:(CLLocationCoordinate2D)coordinate
{
self = [super init];
if (self){
self.name = cityname;
self.country = countryname;
self.coords = coordinate;
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
我导入到我的mapview:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "tableViewController.h"
#import "City.h" …Run Code Online (Sandbox Code Playgroud) mapkit ×10
ios ×6
swift ×6
mkannotation ×3
mkmapview ×2
swift3 ×2
coordinates ×1
ios6 ×1
ios9 ×1
iphone ×1