使用AVSpeechSynthesizer读取Map上的位置描述 [英] Using AVSpeechSynthesizer to read a description of location on a Map

查看:120
本文介绍了使用AVSpeechSynthesizer读取Map上的位置描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的地图有4或5个点彼此靠近,现在使用AVSpeechSynthesizer我已经得到了它,它会说出位置的名称(也会显示在一个小气泡中)。

My map has 4 or 5 points close to each other and right now using AVSpeechSynthesizer I've got it so that it will say the name of the location (which is also displayed in a little bubble).

我希望它仍能显示泡泡,但点击时我希望它说出我将指定的那个地方的描述。这是我目前的代码:

I want it to still show that bubble but when clicked I want it to say a description of that place that I would have specified. This is my code at the moment:

MapViewAnnotation.h

@interface MapViewAnnotation : NSObject <MKAnnotation> {
    NSString *title;
    CLLocationCoordinate2D coordinate;
    NSString *desc;
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly) NSString *desc;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d initWithDesc:(NSString *)dsc;

MapViewAnnotation.m

@implementation MapViewAnnotation

@synthesize title, coordinate, desc;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d initWithDesc:(NSString *)dsc {
[super init];
title = ttl;
coordinate = c2d;
    desc = dsc;
    return self;

MapViewController.h

@interface MapViewController : UIViewController {
    MKMapView *mapView;

}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) AVSpeechSynthesizer *synthesizer;

MapViewController.m

- (void)viewDidLoad
{

    // Set some coordinates for our position : Cutty Sark
    CLLocationCoordinate2D location;
    location.latitude = (double) 51.482997;
    location.longitude = (double) -0.010072;

    // Royal Observatory
    CLLocationCoordinate2D twoLocation;
    twoLocation.latitude = (double) 51.477805;
    twoLocation.longitude = (double) -0.001430;

    //Royal Naval College
    CLLocationCoordinate2D threeLocation;
    threeLocation.latitude = (double) 51.483344;
    threeLocation.longitude = (double) -0.006799;

    //Queen's House
    CLLocationCoordinate2D fourLocation;
    fourLocation.latitude = (double) 51.481383;
    fourLocation.longitude = (double) -0.003722;

    //National Maritime Museum
    CLLocationCoordinate2D fiveLocation;
    fiveLocation.latitude = (double) 51.481050;
    fiveLocation.longitude = (double) -0.005578;


    // Add Cutty Sark annotation to MapView
    MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Cutty Sark" andCoordinate:location initWithDesc:@"Description about Cutty Sark"];
    [self.mapView addAnnotation:newAnnotation];
    self.mapView.delegate = self;
    [newAnnotation release];


    // Add Royal Observatory annotation to MapView
    newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Royal Observatory" andCoordinate:twoLocation initWithDesc:@"Description about Cutty Sark"];
    [self.mapView addAnnotation:newAnnotation];
    [newAnnotation release];

    // Add Royal Naval College annotation to MapView
    newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Royal Naval College" andCoordinate:threeLocation initWithDesc:@"Description about Cutty Sark"];
    [self.mapView addAnnotation:newAnnotation];
    [newAnnotation release];

    // Add Queen's House annotation to MapView
    newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Queen's House" andCoordinate:fourLocation initWithDesc:@"Description about Cutty Sark"];
    [self.mapView addAnnotation:newAnnotation];
    [newAnnotation release];

    // Add National Maritime Museum annotation to MapView
    newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"National Maritime Museum" andCoordinate:fiveLocation initWithDesc:@"Description about Cutty Sark"];
    [self.mapView addAnnotation:newAnnotation];
    [newAnnotation release];


}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)anView
{
    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:((MapViewAnnotation *)anView).desc];
    [utterance setRate:0.5];
    [synthesizer speakUtterance:utterance];
}


// When a map annotation point is added, zoom to it (1500 range)
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id <MKAnnotation> mp = [annotationView annotation];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500);
    [mv setRegion:region animated:YES];
    //[mv selectAnnotation:mp animated:YES];

}

任何帮助都将不胜感激!

Any help would be appreciated!

编辑
这是NSLog:

EDIT Here is the NSLog:

推荐答案

didSelectAnnotationView 委托方法, anView 参数是 MKAnnotationView

anView 视图 MKAnnotationView MKPinAnnotationView class)对象。

That is, anView is the view (MKAnnotationView or MKPinAnnotationView class) object of the annotation.

它不是注释的模型(您的 MapViewAnnotation 类)对象。

It is not the model (your MapViewAnnotation class) object of the annotation.

要引用视图所用的注释模型实例,请使用视图的注释属性并将其强制转换为你的班级:

To refer to the annotation model instance that the view is for, use the view's annotation property and cast it to your class:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)anView
{
    //Get a reference to the annotation this view is for...
    id<MKAnnotation> annSelected = anView.annotation;

    //Before casting, make sure this annotation is our custom type
    //(and not some other type like MKUserLocation)...
    if ([annSelected isKindOfClass:[MapViewAnnotation class]])
    {
        MapViewAnnotation *mva = (MapViewAnnotation *)annSelected;

        AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];

        AVSpeechUtterance *utterance = 
            [AVSpeechUtterance speechUtteranceWithString:mva.desc];

        [utterance setRate:0.5];
        [synthesizer speakUtterance:utterance];
    }
}

这篇关于使用AVSpeechSynthesizer读取Map上的位置描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆