关于苹果的KMLViewer placemarkDescription和注释字幕 [英] Regarding Apple's KMLViewer placemarkDescription and annotation subtitle
问题描述
在我的应用我使用苹果的KMLViewer显示批注,我从一个KML file.In文件KMLParser.m得到的,还有一个实例变量, placemarkDescription
的在转换从KML文件注释subtitle.Now描述标签中的信息,在我的文件中的每个注释有下说明存储这样的信息:
In my app I am using Apple's KMLViewer to show annotations that I get from a KML file.In the file KMLParser.m, there is an instance variable, placemarkDescription
that converts the information under Description tags from kml file to annotation subtitle.Now, in my file every annotation has the information stored under Description in this way:
<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
从这样的:
In KMLParser.m i have transformed the placemarkDescription
from that to this:
<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>
因为我想考绩该字符串网页视图,并在它想象这个我已经做到了这一点。
问题是,当KML负载,方法得到的描述信息,得到所谓严重times.Exactly随着时代的存储在kml.So标直接传递字符串没有effect.If我选择设置活动字幕选项( annotation.subtitle = placemarkDescription
在KMLParser),也许我根获得注释的用户挖掘的字幕信息,但我不想显示这个信息,因为它显示像这种
I've done this because I want to pas this string to a webView and visualize this in it.
The problem is that when the kml loads, the methods get the description information, get called severe times.Exactly as times as placemarks stored in the kml.So passing the string directly has no effect.If i choose to set active the subtitle option (annotation.subtitle = placemarkDescription
in KMLParser), maybe I gen get the subtitle info of the annotation the user tapped, but I don't want to show this information because it shows like this
<table width="280px"><tr><td></td><td></td></tr></table><table width="280px"><tr......
顺便说一句,我没有如何获得所选择的注释的字幕信息的任何想法。
到目前为止,我已成功只存储在一个数组的描述信息(在KMLParser.m做到了这一点),但我应该与数组做什么?如何知道哪个数组项所对应的用户挖掘注释(注释已打开标注泡沫)。
By the way, I don't have any idea of how to get the subtitle info of the selected annotation. So far, I have managed only to store the description information in an array (done this in the KMLParser.m).But what should I do with that array?How to know to which array entry corresponds the annotation the user tapped (the annotation that has the callout bubble opened).
所以我不知道该怎么办。
也许我还没有太明确的:我想要做的就是地标(注释),当用户点击一个注解的地图,拍打disclosureButton应他重定向到一个web视图显示描述信息的描述信息。
So I don't know what to do. Maybe I have not been too clear: What I want to do is get the Description information of a placemark (annotation), when the user taps an annotation in the map, tapping the disclosureButton should redirect him to a webView that shows the description Information.
修改 $ C $下加入:
EDIT code Added:
DetailViewController.h
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
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
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
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更改
Changes in 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;
}
}
错误
推荐答案
目前还不清楚有多少KMLViewer示例应用程序$ C $的c您正在使用,但要做到这一点的方法之一是创建自己的注解类,而不是使用 MKPointAnnotation
如示例应用程序类做。
It's not clear how much of the KMLViewer sample app code you're using but one way to do this is to create your own annotation class instead of using the MKPointAnnotation
class like the sample app does.
自定义类(如 PlacemarkAnnotation
),应实施 MKAnnotation
协议或者是子类 MKShape
(如果你使用的是KMLViewer code)。在自定义类中,添加 placemarkDescription
属性。
The custom class (eg. "PlacemarkAnnotation
"), should implement the MKAnnotation
protocol or be a sub-class of MKShape
(if you are using the KMLViewer code). In the custom class, add a placemarkDescription
property.
凡KMLViewer code目前正在创建一个 MKPointAnnotation
对象,创建一个 PlacemarkAnnotation
代替,并设置其 placemarkDescription
属性而不是字幕
属性。
Where the KMLViewer code currently creates an MKPointAnnotation
object, create a PlacemarkAnnotation
instead and set its placemarkDescription
property instead of the subtitle
property.
然后在 viewForAnnotation
委托方法,将 rightCalloutAccessoryView
来一个细节披露按钮。
Then in the viewForAnnotation
delegate method, set the rightCalloutAccessoryView
to a detail disclosure button.
接下来,将其添加到项目中的细节视图控制器与的UIWebView
。一个 placemarkDescription
属性添加到视图控制器。在 viewDidLoad中
方法,调用 loadHTMLString
上的Web视图,并把它传递 placemarkDescription
(我想你可以通过无
为 BASEURL
)。
Next, add to the project a detail view controller with a UIWebView
in it. Add a placemarkDescription
property to the view controller. In the viewDidLoad
method, call loadHTMLString
on the web view and pass it placemarkDescription
(I think you can pass nil
for the baseURL
).
在地图视图的 calloutAccessoryControlTapped
委托方法,创建细节视图控制器,设置其 placemarkDescription
财产和present是:
In the map view's calloutAccessoryControlTapped
delegate method, create the detail view controller, set its placemarkDescription
property and present it:
-(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
协议。在KMLViewer code的其余部分是基于这样的假设。因此,改变 @interface PlacemarkAnnotation2:NSObject的&LT; MKAnnotation&GT;
到 @interface PlacemarkAnnotation2:MKShape
。 (顺便说一句,对于的NSString
属性,复制
比是比较合适的保留
,它会摆脱警告。)
First, it looks like it will be best to subclass MKShape
for your custom class instead of implementing the MKAnnotation
protocol. The rest of the KMLViewer code is based on this assumption. So change @interface PlacemarkAnnotation2 : NSObject <MKAnnotation>
to @interface PlacemarkAnnotation2 : MKShape
. (By the way, for the NSString
properties, copy
is more appropriate than retain
and it'll get rid of warnings.)
它也像您可以在 KMLPlacemark
已经改变了 mkShape
伊娃的类型(和其他地方)从 MKShape
到别的东西。更改这些类型的回 MKShape
。
It also looks like you may have changed the type of the mkShape
ivar in KMLPlacemark
(and other places) from MKShape
to something else. Change these types back to MKShape
.
接下来, _createShape
可能无法设置最佳场所 placemarkDescription
因为该方法被调用为覆盖和注释。从该方法删除更改并把它们放在点
方法(也 KMLPlacemark
)。注意,有自己的更改了几个潜在的内存相关的问题。这里是我的建议是:
Next, _createShape
might not be the best place to set the placemarkDescription
since that method is called for both overlays and annotations. Remove your changes from that method and put them in the point
method (also in KMLPlacemark
). Note there are a couple of potential memory-related issues with your changes. Here's my suggestion:
- (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;
}
这篇关于关于苹果的KMLViewer placemarkDescription和注释字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!