如何从任何城市或地名设置地图位置

时间:2012-07-27 05:44:22

标签: iphone ios google-maps

这是我获取当前位置的方式

    NSString* lat = [[NSString alloc]initWithFormat:@"%f",locationManager.location.coordinate.latitude];
NSString* lon = [[NSString alloc]initWithFormat:@"%f",locationManager.location.coordinate.longitude];

    NSString *URL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,%@&output=csv&sensor=false",lat,lon];
    NSURLRequest *rq = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
    con = [[NSURLConnection alloc] initWithRequest:rq delegate:self];
    webData = [[NSMutableData data] retain];

在连接委托方法中,我获取当前位置名称。 200,9,“IITI Computer Education,Wadala,Mumbai,Maharashtra 400067,India”在模拟器中。

现在我的问题是我们如何使用我之前获得的字符串地名在地图的同一个地方设置注释。

提前致谢

1 个答案:

答案 0 :(得分:0)

首先制作一个客观的c类,使你的.h文件看起来像下面的

 #import <Foundation/Foundation.h>
 #import <Mapkit/MKAnnotation.h>
 @interface annotation : NSObject <MKAnnotation>

 {
  CLLocationCoordinate2D coordinate;
  NSString *title;
  NSString *subtitle;

 }

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

  @end

现在在.m文件

#import "annotation.h"

@implementation annotation

@synthesize  coordinate,title,subtitle;

@end

现在在你想要添加注释的controller.m文件中导入上面的文件

#import "annotation.h"

现在您可以使用以下函数将anotation添加到任何cordinate

-(void)addAnnotations{

annotation *userPositionAnnote = [[annotation alloc]init];
userPositionAnnote.title = @"My Current Position";
userPositionAnnote.coordinate = SetYourCordinates; // set your cordinates here where you want to add annotaion

[YourMap addAnnotation:userPositionAnnote];

MKCoordinateRegion region = {{0.0,0.0}, {0.0,0.0}};
region.center = userPositionAnnote.coordinate;
region.span.longitudeDelta = 0.001;
region.span.latitudeDelta = 0.001;

[YourMap setRegion:region animated:YES];
}

希望这会对你有所帮助