Using AVSpeechSynthesizer to read a description of location on a Map

后端 未结 1 1270
清酒与你
清酒与你 2021-01-25 11:35

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 litt

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-25 12:06

    In the didSelectAnnotationView delegate method, the anView parameter is the MKAnnotationView.

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

    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 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];
        }
    }
    

    0 讨论(0)
提交回复
热议问题