- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我使用 Apple 的 KMLViewer 来显示我从 KML 文件中获取的注释。在文件 KMLParser.m 中,有一个实例变量 placemarkDescription
用于转换 Description 标签下的信息从 kml 文件到注释副标题。现在,在我的文件中,每个注释都以这种方式在描述下存储信息:
<table width="280px"><tr><td></td><td></td></tr></table><table width="280px"><tr><td><b>Fitness Bulls</b>---Palester sportive. Sporti dhe koha e lire.....<a href="http://www.site.com/BIZ_DIR/810180432/Article-Fitness-Bulls.aspx" style="color:Green;" >Shikoni detajet >></a></td></tr><tr><td><a href="http://www.site.com/HartaV2/AddReview.aspx?gisDataId=8123855e-b798-40bc-ad2e-00346a931211" style="color:Green;" >Shkruani pershtypjen tuaj >> </a> <p style="float:right;">Postuar nga:<i>Import</i></p></td></tr></table>
在 KMLParser.m 中,我已经将 placemarkDescription
从那个转换成这个:
<html><body>
<table width="280px"><tr><td></td><td></td></tr></table><table width="280px"><tr><td>
<b>Fitness Bulls</b>---Palester sportive. Sporti dhe koha e lire.....
<a href="http://www.site.com/BIZ_DIR/810180432/Article-Fitness-Bulls.aspx" style="color:Green;" >Shikoni detajet >></a></td></tr><tr><td>
<a href="http://www.site.com/HartaV2/AddReview.aspx?gisDataId=8123855e-b798-40bc-ad2e-00346a931211" style="color:Green;" >Shkruani pershtypjen tuaj >> </a> <p
style="float:right;">Postuar nga:<i>Import</i></p></td></tr></table>
</body></html>
我这样做是因为我想将此字符串传递给 webView 并在其中可视化。问题是当 kml 加载时,方法获取描述信息,被调用严重时间。与存储在 kml 中的地标的时间完全相同。因此直接传递字符串无效。如果我选择设置事件字幕选项( annotation.subtitle = placemarkDescription
在 KMLParser 中),也许我生成了用户点击的注释的字幕信息,但我不想显示此信息,因为它显示如下
<table width="280px"><tr><td></td><td></td></tr></table><table width="280px"><tr......
顺便说一句,我不知道如何获取所选注释的字幕信息。到目前为止,我只设法将描述信息存储在一个数组中(在 KMLParser.m 中完成)。但是我应该如何处理该数组?如何知道哪个数组条目对应于用户点击的注释(注释标注气泡已打开)。
所以我不知道该怎么办。也许我还不太清楚:我想做的是获取地标(注释)的描述信息,当用户点击 map 中的注释时,点击 disclosureButton 应该将他重定向到显示描述信息的 webView。
编辑代码添加:
DetailViewController.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController<UIWebViewDelegate> {
UIWebView *webView;
UITextField *addressBar;
UIActivityIndicatorView *activityIndicator;
NSString *placemarkDescription;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) IBOutlet UITextField *addressBar;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
@property (nonatomic, retain) NSString *placemarkDescription;
-(IBAction) gotoAddress:(id)sender;
-(IBAction) goBack:(id)sender;
-(IBAction) goForward:(id)sender;
@end
DetailViewController.m
#import "DetailViewController.h"
@implementation DetailViewController
@synthesize webView, addressBar, activityIndicator, placemarkDescription;
- (void)viewDidLoad {
[super viewDidLoad];
[webView loadHTMLString:placemarkDescription baseURL:nil];
}
-(IBAction)gotoAddress:(id) sender {
NSURL *url = [NSURL URLWithString:[addressBar text]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[addressBar resignFirstResponder];
}
-(IBAction) goBack:(id)sender {
[webView goBack];
}
-(IBAction) goForward:(id)sender {
[webView goForward];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:@"http"]) {
[addressBar setText:[URL absoluteString]];
[self gotoAddress:nil];
}
return NO;
}
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[activityIndicator stopAnimating];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[super dealloc];
}
@end
PlacemarkAnnotation2.h
#import <Foundation/Foundation.h>
#import <MapKit/Mapkit.h>
@interface PlacemarkAnnotation2 : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString * title;
NSString * subtitle;
NSString * placemarkDescription;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * subtitle;
@property (nonatomic, retain) NSString * placemarkDescription;
@end
PlacemarkAnnotation2.m
#import "PlacemarkAnnotation2.h"
@implementation PlacemarkAnnotation2
@synthesize coordinate, title, subtitle, placemarkDescription;
- (id) initWithCoordinate:(CLLocationCoordinate2D)coord andTitle:(NSString *)maintitle andSubtitle:(NSString *)subTitle {
self.coordinate = coord;
self.title = maintitle;
self.subtitle = subTitle;
return self;
}
-(NSString *) placemarkDescription
{
return placemarkDescription;
}
- (void) setPlacemarkDescription: (NSString *) pd
{
placemarkDescription = pd;
}
- (void) dealloc {
[title dealloc];
[subtitle dealloc];
[placemarkDescription dealloc];
[super dealloc];
}
@end
KMLParser.M 的变化
//KMLPoint class
- (MKShape *)mapkitShape
{
PlacemarkAnnotation2 *annotation = [[PlacemarkAnnotation2 alloc] init];
annotation.coordinate = point;
return [annotation autorelease];
}
//KMLPlacemark class
- (void)_createShape
{
if (!mkShape) {
mkShape = [[geometry mapkitShape] retain];
mkShape.title = name;
// Skip setting the subtitle for now because they're frequently
// too verbose for viewing on in a callout in most kml files.
NSString *lessThan = @"<";
NSString *greaterThan = @">";
placemarkDescription = [placemarkDescription stringByReplacingOccurrencesOfString:lessThan
withString:@"<"];
placemarkDescription = [placemarkDescription stringByReplacingOccurrencesOfString:greaterThan
withString:@">"];
NSString *beforeBody = @"<html><body>";
NSString *afterBody = @"</body></html>";
NSString *finalContent = [[beforeBody stringByAppendingString:placemarkDescription]
stringByAppendingString:afterBody];
placemarkDescription = finalContent;
mkShape.placemarkDescription = placemarkDescription;
}
}
在这行代码中发现错误(没有崩溃原因描述):
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
DetailViewController *dvc = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
PlacemarkAnnotation2 *pa = (PlacemarkAnnotation2 *)view.annotation;
dvc.placemarkDescription = pa.placemarkDescription;
[self presentModalViewController:dvc animated:YES];
[dvc release];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
最佳答案
不清楚您使用了多少 KMLViewer 示例应用程序代码,但一种方法是创建您自己的注释类而不是使用 MKPointAnnotation
类就像示例应用程序一样。
自定义类(例如“PlacemarkAnnotation
”)应该实现MKAnnotation
协议(protocol)或成为 MKShape
的子类(如果您使用的是 KMLViewer 代码)。在自定义类中,添加一个 placemarkDescription
属性(property)。
KMLViewer 代码当前创建 MKPointAnnotation
的位置对象,创建一个 PlacemarkAnnotation
而是设置它的 placemarkDescription
属性而不是 subtitle
属性(property)。
然后在viewForAnnotation
委托(delegate)方法,设置rightCalloutAccessoryView
到详细信息披露按钮。
接下来,向项目中添加一个带有 UIWebView
的细节 View Controller 在里面。添加 placemarkDescription
View Controller 的属性。在viewDidLoad
方法,调用 loadHTMLString
在网络上查看并传递它placemarkDescription
(我认为您可以将 nil
传递给 baseURL
)。
在 map View 中的 calloutAccessoryControlTapped
委托(delegate)方法,创建细节 View Controller ,设置它的placemarkDescription
属性并呈现它:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
DetailViewController *dvc = [[DetailViewController alloc] init...
PlacemarkAnnotation *pa = (PlacemarkAnnotation *)view.annotation;
dvc.placemarkDescription = pa.placemarkDescription;
[self presentModalViewController:dvc animated:YES];
[dvc release];
}
编辑:
首先,看起来最好将 MKShape
子类化为您的自定义类而不是实现 MKAnnotation
协议(protocol)。 KMLViewer 代码的其余部分基于此假设。所以改变@interface PlacemarkAnnotation2 : NSObject <MKAnnotation>
至 @interface PlacemarkAnnotation2 : MKShape
. (顺便说一句,对于 NSString
属性,copy
比 retain
更合适,它会消除警告。)
看起来您可能已经更改了 mkShape
的类型ivar 在 KMLPlacemark
(和其他地方)来自 MKShape
到别的东西。将这些类型改回 MKShape
.
接下来,_createShape
可能不是设置 placemarkDescription
的最佳位置因为覆盖和注释都调用了该方法。从该方法中删除您的更改并将它们放入 point
方法(也在 KMLPlacemark
中)。请注意,您的更改存在一些与内存相关的潜在问题。这是我的建议:
- (void)_createShape
{
if (!mkShape) {
mkShape = [[geometry mapkitShape] retain];
mkShape.title = name;
// Skip setting the subtitle for now because they're frequently
// too verbose for viewing on in a callout in most kml files.
}
}
- (id <MKAnnotation>)point
{
[self _createShape];
if ([mkShape isKindOfClass:[PlacemarkAnnotation2 class]])
{
if (placemarkDescription != nil)
//check for nil, otherwise will crash when
//passing to stringByAppendingString below
{
NSString *lessThan = @"<";
NSString *greaterThan = @">";
placemarkDescription = [placemarkDescription stringByReplacingOccurrencesOfString:lessThan
withString:@"<"];
placemarkDescription = [placemarkDescription stringByReplacingOccurrencesOfString:greaterThan
withString:@">"];
NSString *beforeBody = @"<html><body>";
NSString *afterBody = @"</body></html>";
NSString *finalContent = [[beforeBody stringByAppendingString:placemarkDescription]
stringByAppendingString:afterBody];
placemarkDescription = [finalContent retain];
//added retain above since finalContent is autoreleased
//and we are setting the ivar manually. otherwise,
//can result in EXC_BAD_ACCESS later.
}
PlacemarkAnnotation2 *pa2 = (PlacemarkAnnotation2 *)mkShape;
pa2.placemarkDescription = placemarkDescription;
return (id <MKAnnotation>)mkShape;
}
return nil;
}
关于iphone - 关于Apple的KMLViewer placemarkDescription and annotation subtitle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8285153/
在我的应用程序中,我使用 Apple 的 KMLViewer 来显示我从 KML 文件中获取的注释。在文件 KMLParser.m 中,有一个实例变量 placemarkDescription 用于转
我是一名优秀的程序员,十分优秀!