2014-11-22 48 views
0

我正在使用GoogleMaps在iOS中。根據文本動態獲取地圖位置

我成功地指着由靜態使用此代碼

給出的地址 - (無效)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上的位置的文本(地址)

回答

0

我寫了自定義m ethod

- (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; 
} 

OUTPUT:

當過我傳遞的字符串,然後自動指向的GoogleMap的居民點。