我尝试在swift中为我的MKPointAnnotation创建一个自定义的"badget",但它失败了,因为MKPointAnnotation没有像图像那样的任何属性
var information = MKPointAnnotation()
information.coordinate = location
information.title = "Test Title!"
information.subtitle = "Subtitle"
information.image = UIImage(named: "dot.png") //this is the line whats wrong
Map.addAnnotation(information)
Run Code Online (Sandbox Code Playgroud)
有人想出了一个快速的解决方案吗?
是否可以添加额外的属性MKPointAnnotation?目前有coordinate,title和subtitle.
是否可以添加一个url可以访问的属性-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{ },就像我可以访问一样[annotation title];?
我有一个带注释的mapView,显示标题和副标题.字幕有时长于注释的宽度,所以我想知道我是否可以使它们成为多线?它到目前为止编码如此:
func annotate(newCoordinate, title: String, subtitle: String) {
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = title
annotation.subtitle = subtitle
self.map.addAnnotation(annotation)
}
Run Code Online (Sandbox Code Playgroud)
然后我有几个选项
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {...}
Run Code Online (Sandbox Code Playgroud)
这里没有关系.
是否可以制作自定义注释视图?我尝试了几件事,但没有任何效果.我能得到的最接近的是添加一个按钮来分别显示较长的字幕,但我宁愿把它放在注释中.
可能吗?
我的问题MKPointAnnotation在地图上丢失了.我会解释一下.
场景:
从服务器读取JSON - >在地图上删除引脚 - >单击某个引脚时,打开一个带有传递给视图的参数的新视图.该参数必须绑定到引脚.
我的代码到目前为止:
JSON(仅限示例用途)
{"places":[{"lat":"30.03","lng":"40.31","id":"1"}]}
Run Code Online (Sandbox Code Playgroud)
读取JSON并向地图添加点:
NSString *markersJSON=@"http://test.com/json.php";
NSURLRequest *requestUsername = [NSURLRequest requestWithURL:[NSURL URLWithString:markersJSON]];
NSData *response = [NSURLConnection sendSynchronousRequest:requestUsername
returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response
options:0 error:&jsonParsingError];
markerArray = [json objectForKey:@"places"];
for (NSDictionary *jsonObj in markerArray ){
latJSON=[jsonObj objectForKey:@"lat"];
lngJSON=[jsonObj objectForKey:@"lng"];
alertID=[jsonObj objectForKey:@"id"];
CLLocationCoordinate2D myCoordinate = {[latJSON doubleValue], [lngJSON doubleValue]};
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = myCoordinate;
point.title=[jsonObj objectForKey:@"title"];
point.subtitle=[jsonObj objectForKey:@"description"];
[self.mapView addAnnotation:point];
}
Run Code Online (Sandbox Code Playgroud)
单击注释时,使用与引脚对应的JSON中的变量"id"打开新视图.
- …Run Code Online (Sandbox Code Playgroud) 我创建了一个地图,MKPointAnnotation其中有一个地图上的单点(除了用户位置).我试图找出如何修改我现有的一些代码来获得这方面的驾驶方向.
这是我在应用程序早期使用的代码.在应用程序的早期阶段,我有以下内容,它给了我一个CLPlaceMark.
[geocoder geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
收集方向:
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
[request setSource:[MKMapItem mapItemForCurrentLocation]];
MKPlacemark *mkDest = [[MKPlacemark alloc] initWithPlacemark:topResult];
[request setDestination:[[MKMapItem alloc] initWithPlacemark:mkDest]];
[request setTransportType:MKDirectionsTransportTypeWalking]; // This can be limited to automobile and walking directions.
[request setRequestsAlternateRoutes:NO];
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if (!error) {
for (MKRoute *route in [response routes]) {
[self.mapView addOverlay:[route …Run Code Online (Sandbox Code Playgroud) ios ×5
mapkit ×3
swift ×2
cocoa-touch ×1
mkannotation ×1
mkmapitem ×1
mkmapview ×1
objective-c ×1