动态获取基于文本的地图位置

时间:2014-11-22 14:38:43

标签: ios google-maps

我正在iOS中使用GoogleMaps。

我成功地使用此代码

指向静态给出的位置

- (无效)pointingLocation {

//code5.1
//Donepudi my hometown
GMSCameraPosition * cameraPosition = [GMSCameraPosition cameraWithLatitude:
                                      16.149319 longitude:80.817017 zoom:6];
//itouchmap.com for accessig Latitude and longitude

//code5.2
//for set camera postion
mapView = [GMSMapView mapWithFrame:CGRectZero camera:cameraPosition];

//code5.3
//for privacy reasons our location is deisabled.
mapView.myLocationEnabled = NO;//default no.

//code5.4
//set self view as a map view
self.view = mapView;

//code5.5
/**
 * A marker is an icon placed at a particular point on the map's surface. A
 * marker's icon is drawn oriented against the device's screen rather than the
 * map's surface; i.e., it will not necessarily change orientation due to map
 * rotations, tilting, or zooming.
 */
GMSMarker *marker = [[GMSMarker alloc]init];

//PINPOINT LOCATION IS SET TO OUR REQUIREMENT.
marker.position = CLLocationCoordinate2DMake(16.149319, 80.817017);

//SET TITLE SHOWS TOP OF THE PIN POINT
marker.title = @"Donepudi";

//SET SNNIPET
marker.snippet = @"Donepudi,INIDIA";

//SET OVERLAY VIEW
marker.map = mapView;

}

但我想动态设置文本(地址) 基于指向mapview上的位置的文本(地址)

1 个答案:

答案 0 :(得分:0)

我写了自定义方法

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{

    double latitude = 0, longitude = 0;

    NSString *esc_addr =  [address  
                             stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSString *req = [NSString  

      stringWithFormat:@"http://maps.google.com/maps/api/geocode/json             
                                   sensor=false&address=%@", esc_addr];

    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] 
                                         encoding:NSUTF8StringEncoding error:NULL];
    if (result) {

        NSScanner *scanner = [NSScanner scannerWithString:result];

        if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && 
            [scanner scanString:@"\"lat\" :" intoString:nil]) {

                   [scanner scanDouble:&latitude];

                   if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && 
                       [scanner scanString:@"\"lng\" :" intoString:nil]) {

                                 [scanner scanDouble:&longitude];
                   }
        }
    }
    CLLocationCoordinate2D center;
    center.latitude = latitude;
    center.longitude = longitude;

    return center;
}

输出:

当我传递字符串然后自动指向GoogleMap中的所需位置时。