2011-08-24 25 views
7

görüntü biriminin bir parçası olun Bir yerde, benim yerimin çizileceği alanın sadece bir kısmını göstermek zorunda olduğum bir uygulama yaratıyorum, Aşağıdakine benzer. Bu irade görüntü üzerindeHarita görünümünün bir parçası olarak iOS

MAp part

tıklamak ayrıca Uygulamamı alacaktır. Ama burada, enlem ve boylamıma bağlı olarak oluşan statik bir görüntü.

Herhangi bir yardım gerçekten takdir edilecektir. Teşekkürler

cevap

35

İşte çalışma kodu. Herkese hala cevabı aramaya devam edin.

NSString *staticMapUrl = [NSString stringWithFormat:@"http://maps.google.com/maps/api/staticmap?markers=color:red|%f,%f&%@&sensor=true",yourLatitude, yourLongitude,@"zoom=10&size=270x70"];  
NSURL *mapUrl = [NSURL URLWithString:[staticMapUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:mapUrl]]; 

Teşekkür

+0

benim için çalışmasına teşekkürler ve zamanımı kurtar. –

+0

pin image edit edebilirim –

-2

Açık olmak gerekirse, harita setini ilgili yere ilişkin bir görüntü elde etmek için bir yolu yoktur.

Sen

+1

Aşağıda bir kod belirtilmiştir. –

3

Sen Burada örnek kod

var Google Static Maps API

deneyebilirsiniz .. Eğer yapmak istediği her şeyi yapmaya notu üzerine tıklayın kaydırma/yakınlaştırma kullanıcıyı devre dışı bırakmak, MKMapView kullanmak ek açıklama eklemek ve işlemek zorunda

NSString *urlString = @"http://maps.googleapis.com/maps/api/staticmap?center=Berkeley,CA&zoom=14&size=200x200&sensor=false"; 
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]; 
UIImage *imageData = [UIImage imageWithData:data]; 
[yourImageView setImage:imageData]; 

Bu sayede koordinatlarınıza göre statik görüntü alabilirsiniz. Ancak, ek açıklamaların özelleştirilmesinin mümkün olmayabileceğini söylemekten korkuyorum.

3

Ayrıca MKMapSnapshotter kullanabilirsiniz, bu haritanın ekran görüntüsünü almak için izin verir;

MKMapSnapshotOptions * snapOptions= [[MKMapSnapshotOptions alloc] init]; 
     self.options=snapOptions; 
     CLLocation * Location = [[CLLocation alloc] initWithLatitude:self.lat longitude:self.long]; 
     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(Location.coordinate, 300, 300); 
     self.options.region = region; 
     self.options.size = self.view.frame.size; 
     self.options.scale = [[UIScreen mainScreen] scale]; 

     MKMapSnapshotter * mapSnapShot = [[MKMapSnapshotter alloc] initWithOptions:self.options]; 

     [mapSnapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) { 
      if (error) { 
       NSLog(@"[Error] %@", error); 
       return; 
      } 

      UIImage *image = snapshot.image;//map image 
      NSData *data = UIImagePNGRepresentation(image); 

     }]; 
+0

Property 'options' nesne türünde bulunmuyor –

+0

@ShahbazAkram'da MKMapSnapShotOptions denilen bir özelliği bildirmelisiniz – DevC

0

Görüntünün ortasında bir açıklama ekleyen bir yöntem (tamamlama bloğuyla) sağlayarak @DevC yanıtına eklemek istiyorum. İlk başta, anlık görüntü başlamadan önce ek açıklama ekleyip eklemediğimi görmek istedim, ancak MKMapSnapshotternot support this. Bu yüzden, bulduğum çözüm, bir iğne oluşturmak, harita görüntüsünün üstüne çizmek ve daha sonra haritanın + iğnesinin başka bir görüntüsünü oluşturmaktır. Ben harita görüntüsüne ek açıklamaları eklemek isteyen insanlara bu bazı kullanım umut

typedef void(^map_screenshot_completion)(UIImage *); 

: Bu yöntem aynı zamanda şöyle beyan edilecek bir tamamlanma başlığı gerektirir

-(void)create_map_screenshot:(MKCoordinateRegion)region :(map_screenshot_completion)map_block { 

    // Set the map snapshot properties. 
    MKMapSnapshotOptions *snap_options = [[MKMapSnapshotOptions alloc] init]; 
    snap_options.region = region; 
    snap_options.size = // Set the frame size e.g.: custom_view.frame.size; 
    snap_options.scale = [[UIScreen mainScreen] scale]; 

    // Initialise the map snapshot camera. 
    MKMapSnapshotter *map_camera = [[MKMapSnapshotter alloc] initWithOptions:snap_options]; 

    // Take a picture of the map. 
    [map_camera startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) { 

     // Check if the map image was created. 

     if ((error == nil) && (snapshot.image != nil)) { 

      // Create the pin image view. 
      MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:nil]; 

      // Get the map image data. 
      UIImage *image = snapshot.image; 

      // Create a map + location pin image. 
      UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale); { 

       [image drawAtPoint:CGPointMake(0.0f, 0.0f)]; 
       CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height); 

       // Create the pin co-ordinate point. 
       CGPoint point = [snapshot pointForCoordinate:region.center]; 

       if (CGRectContainsPoint(rect, point)) { 

        // Draw the pin in the middle of the map. 
        point.x = (point.x + pin.centerOffset.x - (pin.bounds.size.width/2.0f)); 
        point.y = (point.y + pin.centerOffset.y - (pin.bounds.size.height/2.0f)); 
        [pin.image drawAtPoint:point]; 
       } 
      } 

      // Get the new map + pin image. 
      UIImage *map_plus_pin = UIGraphicsGetImageFromCurrentImageContext(); 

      // Return the new image data. 
      UIGraphicsEndImageContext(); 
      map_block(map_plus_pin); 
     } 

     else { 
      map_block(nil); 
     } 
    }]; 
} 

: İşte benim özel yöntemidir .

İlgili konular