如何在DetailView中传递发件人标记以获取默认的Map应用程序以进行指示? [英] How to pass sender tag in DetailView to get default Map application for direction?
问题描述
我正在打开注释图钉和表格视图上的Detailview.为了从detailview获得方向,我在按钮单击事件之后放置了地方.
I am opening the Detailview on annotation pin as well as on tableview. To get direction from detailview I have place following button click event.
已编辑 ::带有-(IBAction)showDirectionUpdated;编码
Edited:: with -(IBAction)showDirectionUpdated; coding
-(IBAction)showDirectionUpdated;
{
NSIndexPath *selectedIndexPath = [self._tableView indexPathForSelectedRow];
if( selectedIndexPath == [self._tableView indexPathForSelectedRow])
{
marker *aMarker = (marker *)[appDelegate.markers objectAtIndex:selectedIndexPath.row];
NSString *EndLoc=[NSString stringWithFormat:@"%@ %@", aMarker.address,aMarker.city];
NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=Current Location&daddr=%@",EndLoc];
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
NSLog(@"%@",url);
[url release];
[self._tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
}
else
{
//here if I am opening the detailview from annotation callout button and calling
// direction in map default app. But respective address is not passing
//in default map app
NSInteger selectedIndex = [sender tag];
AddressAnnotation *selectedObject = [self.annobjs objectAtIndex:selectedIndex];
marker *aMarker = [appDelegate.markers objectAtIndex:selectedIndex];
NSString *EndLoc=[NSString stringWithFormat:@"%@ %@", aMarker.address,aMarker.city];
NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=Current Location&daddr=%@",EndLoc];
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
NSLog(@"%@",url);
[url release];
}
}
我想传递一些发件人或ID来从detailView中调用相应的Direction,而我在按注时会收到该信息.我成功地通过选择tableview(listview)从detailview获取方向默认应用程序.这里是一些发件人标签代码.
I want to pass some sender or id to call respective Direction from detailView which I get on pressing annotation. I am successful with getting direction default app from detailview which I get by selecting tableview(listview). Here some code of sender tag.
使用viewForAnnotation编辑2 ===
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{ MKAnnotationView *annView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@""];
if (annView == nil)
{
annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""] autorelease];
annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
annView.image = [UIImage imageNamed:@"flag.png"];
annView.annotation = annotation;
[annView setEnabled:YES];
[annView setCanShowCallout:YES];
return annView;
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"calloutAccessoryControlTapped");
MKAnnotationView* annView = (MKAnnotationView*) view;
AddressAnnotation* annotation = (AddressAnnotation*)[annView annotation];
if(BcardView.hidden == YES)
{
SearchView.hidden = YES;
BcardView.hidden = NO;
BackButtontitle.text = @"Map View";
marker *aMarker = [[marker alloc]init];
ShowroomName.text = aMarker.name;
}}
推荐答案
基于发布的代码,这是我的理解:
Based on the code posted, here is my understanding:
marker
是存储有关位置的所有详细信息(包括address
和city
)的顶级类.
marker
is the top-level class that stores all the details about a location including address
and city
.
AddressAnnotation
是实现MKAnnotation
协议的类,该协议当前仅包含位置的坐标,标题和副标题(不具有address
和city
).这是用于创建传递到地图视图的addAnnotation
方法的对象的类.
AddressAnnotation
is the class that implements the MKAnnotation
protocol currently containing just the coordinate, title, and subtitle for a location (it does not have the address
and city
). This is the class used to create objects that are passed to the map view's addAnnotation
method.
您想要做的是,当在注释上按下标注按钮时,您想要显示一些需要address
和city
的详细视图或信息.
What you would like to do is that when the callout button is pressed on an annotation, you would like to show some detail view or information that requires the address
and city
.
我建议不要在标签中跟踪数组索引或在数组中搜索对象,而建议在AddressAnnotation
中添加对marker
的引用.这样,当您拥有一个AddressAnnotation
对象时,可以使用该引用来获取其相关的marker
对象(无需搜索或索引).
Instead of trying to keep track of array indexes using tags or searching for objects in arrays, I suggest adding a reference to the marker
in AddressAnnotation
. This way, when you have an AddressAnnotation
object, you can get its related marker
object using that reference (no searching or indexes needed).
在AddressAnnotation
类中,添加名为parentMarker
的类型为marker
的保留属性.当从marker
创建AddressAnnotation
对象时,在调用addAnnotation
之前,请将parentMarker
属性设置为当前的marker
.
In the AddressAnnotation
class, add a retain property of type marker
called parentMarker
. When you create an AddressAnnotation
object from a marker
and before calling addAnnotation
, set the parentMarker
property to the current marker
.
然后在calloutAccessoryControlTapped
中,将view.annotation
强制转换为AddressAnnotation
,以轻松访问parentMarker
属性.现在,您可以从标记中获取address
和city
. (顺便说一句,在这种方法中,您无需将view
强制转换为MKAnnotationView
,因为它已经是一个,并且不必将其命名为annView
.)
Then in calloutAccessoryControlTapped
, cast view.annotation
to an AddressAnnotation
to get easy access to the parentMarker
property. Now you can get the address
and city
from the marker. (By the way, in that method you don't need to cast view
to MKAnnotationView
since it already is one and you don't have to name it annView
.)
我不确定BcardView
是什么,以及为什么要检查它是否隐藏.
I am not sure what BcardView
is and why you're checking if it's hidden or not.
我还建议对viewForAnnotation
方法进行一点改进(使用非空白的重用id并将未更改的属性的设置移到if
内部):
I'd also suggest a slight improvement to the viewForAnnotation
method (use a non-blank re-use id and move the setting of the properties that don't change to inside the if
):
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>) annotation
{
MKAnnotationView *annView = (MKAnnotationView *)[mapView
dequeueReusableAnnotationViewWithIdentifier:@"AddrAnnot"];
if (annView == nil)
{
annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"AddrAnnot"] autorelease];
annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.image = [UIImage imageNamed:@"flag.png"];
[annView setEnabled:YES];
[annView setCanShowCallout:YES];
}
annView.annotation = annotation;
return annView;
}
这篇关于如何在DetailView中传递发件人标记以获取默认的Map应用程序以进行指示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!