2012-05-31 53 views
1

我正在開發一個簡單的地圖,我想使用公司名稱在地圖上找到公司。例如「Apple Inc.」。如何在地圖上定位公司(mapkit iOS)

你知道我該怎麼做?

非常感謝!

問候,衙署

+0

您需要定義你所說的「一個公司的位置」的意思。我想你的意思是總部地址。獲得地址後,您可以使用mapkit地理編碼API獲取座標。 – Felix

+0

根據Apples的「位置感知編程指南」,您可以使用'CLGeoCoder'獲取'Apple Inc.'的座標。 – pre

回答

1

你需要有實現MKAnnotation協議的類。下面是一個例子

@interface MapPin : NSObject<MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
} 

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

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description; 

@end 

下面是執行:

@implementation MapPin 

@synthesize coordinate; 
@synthesize title; 
@synthesize subtitle; 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:placeName description:description { 
    self = [super init]; 
    if (self != nil) { 
     coordinate = location; 
     title = placeName; 
     [title retain]; 
     subtitle = description; 
     [subtitle retain]; 
    } 
    return self; 
} 

- (void)dealloc { 
    [title release]; 
    [subtitle release]; 
    [super dealloc]; 
} 


@end 

然後,在你的MapView你需要添加公司所在地像下面。

CLLocationCoordinate2D coord = [[[CLLocation alloc] initWithLatitude:35.936902 longitude:-79.024953] coordinate];//Here you need to mention your company latitude and longitude 
MapPin *pin = [[MapPin alloc] initWithCoordinates:coord placeName:@"Apple Inc" description:@""]; 
[map addAnnotation:pin]; 

希望這將有助於....

+0

嗨,Nandha,謝謝你的回覆。但是,我如何得到「在這裏你需要提及你公司的經緯度」?有沒有辦法根據公司名稱得到它?謝謝! – Yashu

+0

您可以使用公司地址獲得公司的經緯度。有一些網站會爲你做這件事,http://stevemorse.org/jcal/latlon.php。或者你可以使用IOS。 – Nandha

+0

但是,如果我只有公司名稱(不是地址)? – Yashu