2015-10-21 18 views
13

マップ上のオーバーレイにぼかし効果を追加しようとしています。私は実際にマップ上の円の上にぼかし効果が必要です、私がそれを得るために使用する方法はそれほど重要ではありません。MKMapViewでぼかし効果を持つ円を追加する

私はMKCircleRendererから拡張されたクラスを持っており、それがカバーするマップ上にぼかし効果を追加したいと考えました。

私は-fillPath:inContext:メソッドを使用しようとしていましたが、Core GraphicsとCore Imageに対する私の無知は私をどこにも導かず、私はこの問題について本当に失われました。

私の試みはCIFilterを使用していましたが、私はコンテキストから作成しようとしたCIImageが必要でした。しかし、私はCGBitmapContextCGImageなどの文脈から他のクラスを作成する方法が見つけられませんでした。私が試みたどのような方法でも、理由についての詳細はないNULLが返されました。私はすべてを覚えていないので、それについて何も指摘しないのは残念です。

- (instancetype)initWithOverlay:(id<MKOverlay>)overlay { 
    if (self = [super initWithOverlay:overlay]) { 
     self.strokeColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1]; 
     self.fillColor = [UIColor colorWithRed:0.4 green:0.2 blue:0.2 alpha:0.1]; 
     self.lineWidth = 1; 
    } 
    return self; 
} 

代替カスタムMKAnnotationを使用してUIVisualEffectViewとビューの上にぼかし効果を追加することができます:

私のクラスでは、現在、本当に多く行っていない1つの方法が実装されています。このアプローチの難しい部分は、ズームするときにサイズを増減することです。

これはiOSの8+

編集この場合

で動作するはずです、円の内側の後ろのマップは次の2つの方法があります

+0

はあなたが達成したい正確に何のスクリーンショットを提供することができますか?特定のサークルを除いて地図をぼかしますか?または円をぼかすだけですか?ズームに依存するのはなぜですか? – luk2302

+0

あなたが必要とするもののスクリーンショットを表示する方が良いでしょう –

+0

円をぼかしてください。私はぼかし円で覆われた領域を持っていたいと思います。この領域の半径は0.25マイルにする必要があります。そのため、ズームイン/ズームアウトするにはサイズを変更する必要があります。もうすぐイメージを追加します。 –

答えて

2

私はオーバーレイの上にUIVisualEffectViewを使用しました。そのトリックは、場所を表示するためにCADisplayLinkを使用していました。

ここでは、仕事を行うコード例をいくつか紹介します(リンクを削除する、viewDidAppearで行われたことをペアにしなければならないことを追跡するなど、おそらくviewWillDissapearや何かに対して対称的な仕事をしていると思いますが、私はviewDidLoadを使うことができました。

#import "ViewController.h" 
#import <MapKit/MapKit.h> 
#import <QuartzCore/QuartzCore.h> 

@interface ViewController() { 
    IBOutlet MKMapView *map; 
    UIView *ov; 
    MKCircle *c; 
    UIVisualEffectView *ev; 
} 

@end 

@implementation ViewController 

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(40.7828647,-73.9675438); 
    c = [MKCircle circleWithCenterCoordinate:center radius:1000]; 
    [map addOverlay:c]; 
    MKCoordinateSpan span = MKCoordinateSpanMake(0.07, 0.07); 
    MKCoordinateRegion region = MKCoordinateRegionMake(center, span); 
    region = [map regionThatFits:region]; 
    [map setRegion:region animated:YES]; 

    ov = [[UIView alloc] init]; 
    ov.translatesAutoresizingMaskIntoConstraints = NO; 
    ov.backgroundColor = [UIColor clearColor]; 
    ov.clipsToBounds = YES; 
    ov.layer.borderWidth = 1; 
    ov.layer.borderColor = [UIColor blackColor].CGColor; 
    [map addSubview:ov]; 

    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; 
    ev = [[UIVisualEffectView alloc] initWithEffect:blur]; 
    [ov addSubview:ev]; 

    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update:)]; 
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; 
} 

- (void)update:(CADisplayLink *)link { 
    ov.frame = [map convertRegion:MKCoordinateRegionForMapRect(c.boundingMapRect) toRectToView:map]; 
    ov.layer.cornerRadius = ov.frame.size.height/2; 
    ev.frame = CGRectMake(0, 0, ov.frame.size.width, ov.frame.size.height); 
} 

@end 

編集:スクリーンショット

2

をぼかすべきですそれ。

1つはシンプルですが、あなたは単にImageを使うことができます。

あなたは以下の画像が有用見つけるかもしれない:

enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

とviewForAnnotationでそれらを使用するコード:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 
    // ... get the annotation delegate and allocate the MKAnnotationView (annView) 
     if ([annotationDelegate.type localizedCaseInsensitiveCompare:@"NeedsBluePin"] == NSOrderedSame) 
     { 
      UIImage * image = [UIImage imageNamed:@"blue_pin.png"]; 
      UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease]; 
      [annView addSubview:imageView]; 
     } 
} 

他の方法をないプログラムで、[サークルを自分で作ること]

このコードはあなたを助けることができます。

import UIKit 
import AddressBook 
import AddressBookUI 
import MapKit 
import CoreLocation 
import MessageUI 
import Social 

class ViewController: UIViewController, ABPeoplePickerNavigationControllerDelegate, MFMailComposeViewControllerDelegate, MKMapViewDelegate { 

    @IBOutlet weak var name: UILabel! 
    @IBOutlet weak var email: UILabel! 
    @IBOutlet weak var photo: UIImageView! 
    @IBOutlet weak var map: MKMapView! 

    let locMan:CLLocationManager=CLLocationManager() 

    // Blurring Code 
    @IBOutlet weak var labelBackground: UIView! 
    var backgroundBlur: UIVisualEffectView! 


    @IBAction func newBFF(sender: AnyObject) { 
     let picker: ABPeoplePickerNavigationController = 
      ABPeoplePickerNavigationController() 
     picker.peoplePickerDelegate = self 
     presentViewController(picker, animated: true, completion: nil) 
    } 

    @IBAction func sendEmail(sender: AnyObject) { 
     var emailAddresses:[String]=[self.email.text!] 
     var mailComposer:MFMailComposeViewController = 
      MFMailComposeViewController() 
     mailComposer.mailComposeDelegate=self; 
     mailComposer.setToRecipients(emailAddresses) 

     presentViewController(mailComposer, animated: true, completion: nil) 
    } 

    func mailComposeController(controller: MFMailComposeViewController!, 
     didFinishWithResult result: MFMailComposeResult, error: NSError!) { 
     dismissViewControllerAnimated(true, completion: nil) 
    } 

    func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!) { 

     let friendName:String = ABRecordCopyValue(person, kABPersonFirstNameProperty).takeRetainedValue() as String as String 
     name.text=friendName 

     let friendAddressSet:ABMultiValueRef = ABRecordCopyValue(person, kABPersonAddressProperty).takeRetainedValue() 

     if ABMultiValueGetCount(friendAddressSet)>0 { 
      let friendFirstAddress: Dictionary = ABMultiValueCopyValueAtIndex(friendAddressSet, 0).takeRetainedValue() as NSDictionary 
      showAddress(friendFirstAddress) 
     } 

     let friendEmailAddresses:ABMultiValueRef = ABRecordCopyValue(person, kABPersonEmailProperty).takeRetainedValue() 

     if ABMultiValueGetCount(friendEmailAddresses)>0 { 
      let friendEmail: String = ABMultiValueCopyValueAtIndex(friendEmailAddresses, 0).takeRetainedValue() as String 
      email.text=friendEmail 
     } 

     if ABPersonHasImageData(person) { 
      photo.image = UIImage(data: ABPersonCopyImageData(person).takeRetainedValue()) 
     } 
    } 

    func showAddress(fullAddress:NSDictionary) { 
     let geocoder: CLGeocoder = CLGeocoder() 
     geocoder.geocodeAddressDictionary(fullAddress, completionHandler: 
      {(placemarks: [AnyObject]!, error: NSError!) -> Void in 
       let friendPlacemark:CLPlacemark = placemarks[0] as CLPlacemark 
       let mapRegion:MKCoordinateRegion = 
        MKCoordinateRegion(center: friendPlacemark.location.coordinate, 
         span: MKCoordinateSpanMake(0.2, 0.2)) 
       self.map.setRegion(mapRegion, animated: true) 
       let mapPlacemark: MKPlacemark = MKPlacemark(placemark: friendPlacemark) 
       self.map.addAnnotation(mapPlacemark) 
     }) 
    } 

    func mapView(aMapView: MKMapView!, 
     viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
      let pinDrop:MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myspot") 
      pinDrop.animatesDrop=true 
      pinDrop.canShowCallout=true 
      pinDrop.pinColor=MKPinAnnotationColor.Purple 
      return pinDrop 
    } 

    @IBAction func sendTweet(sender: AnyObject) { 
     let geocoder: CLGeocoder = CLGeocoder() 
     geocoder.reverseGeocodeLocation(map.userLocation.location, completionHandler: 
      {(placemarks: [AnyObject]!, error: NSError!) -> Void in 
       let myPlacemark:CLPlacemark = placemarks[0] as CLPlacemark 
       let tweetText:String = 
        "Hello all - I'm currently in \(myPlacemark.locality)!" 

       let tweetComposer: SLComposeViewController = 
        SLComposeViewController(forServiceType: SLServiceTypeTwitter) 

       if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter) { 
        tweetComposer.setInitialText(tweetText) 
        self.presentViewController(tweetComposer, animated: true, completion: nil) 
       } 
     }) 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     //let locMan:CLLocationManager=CLLocationManager() 
     locMan.requestWhenInUseAuthorization() 

     let blur: UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light) 
     backgroundBlur = UIVisualEffectView (effect: blur) 
     backgroundBlur.frame = labelBackground.frame 
     view.insertSubview(backgroundBlur, belowSubview: labelBackground) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func preferredStatusBarStyle() -> UIStatusBarStyle { 
     return UIStatusBarStyle.LightContent 
    } 


} 
+1

私は何か間違った表現をしたかもしれませんが、編集上では正しく言います。私は、地図の上に浮かんでいない円の後ろの地図をぼかす必要があります:/とにかくありがとう。 –

+1

また、注釈のビューはズームAFAIKを無視します。ズームインまたはズームアウトすると、ビューのサイズは画面上に表示されますが、地図上に表示されている実際のものとは関係ありません。 –

関連する問題