【问题标题】:How can I add places to Google Places API in iPhone App如何在 iPhone 应用程序中将地点添加到 Google Places API
【发布时间】:2013-03-19 18:03:29
【问题描述】:

我可以使用 Google Places API 搜索地点。 如何使用 Google Places API 添加我的地点。我看过文档 但无法弄清楚。 http://code.google.com/apis/maps/documentation/places/#PlaceSearchRequests 谁能帮帮我。

【问题讨论】:

    标签: iphone ios google-places-api


    【解决方案1】:

    在我的应用程序中,我在 UIsearchbar 中输入位置,然后我调用以下方法

    -(void)getLocation
    {
          NSString *urlString;
            if(locationFinder.text!=nil)
            {
                urlString = [[NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [locationFinder.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]retain];
            }
    
    
                    NSLog(@"url:%@",urlString);
    
                NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
                NSArray *listItems = [locationString componentsSeparatedByString:@","];
    
    
                if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) 
                {
                    latitude = [[listItems objectAtIndex:2] doubleValue];
                    longitude = [[listItems objectAtIndex:3] doubleValue];
                }
    
                NSLog(@"latitude: %f longitude:%f",latitude,longitude);
    
                urlString=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%d&types=%@&sensor=true&key=AIzaSyBbUuE-DprCN-CME1SgcNxyeuDdRrBgkyk",latitude,longitude,mRadius,mTypes];
    
            NSLog(@"url: %@",urlString);
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:180.0];
            id urlRequest = [[NSURLConnection alloc] initWithRequest:request delegate:self];
            if(urlRequest)
            {
                    responseData=[[NSMutableData data]retain];
                    NSLog(@"hiiii i m data");
    
              }
        }
    

    并实现一些其他委托方法并通过 jSON 解析数据

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        [responseData setLength:0];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        //NSLog(@"DATA:%@",data);
        [responseData appendData:data];
        //NSLog(@"%@",responseData);
         //[responseData release];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        NSLog(@"connection failed:%@",[error description]);
        //done.enabled = YES;
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        [connection release];
    
        //NSLog(@"response data:%@",responseData);
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        //NSLog(@"response:%@",responseString);
         //[responseData release];
        NSDictionary *locationData = [responseString JSONValue];
        NSLog(@"ALLKEYS:%@",[locationData allKeys]);
              self.responseDataDict=[locationData objectForKey:@"results"];
              NSLog(@"locationdata allkeys:%@",[locationData allKeys]);
            NSLog(@"name:%d",[responseDataDict count]);
    
    
    
    }
    

    【讨论】:

    • 嗨,谢谢,但我的问题是如何向 Google Places API 添加新地点
    【解决方案2】:

    使用以下示例添加地点。

        POST https://maps.googleapis.com/maps/api/place/add/json?sensor=true_or_false&key=api_key 
        HTTP/1.1
        Host: maps.googleapis.com
    
        {
          "location": {
            "lat": -33.8669710,
            "lng": 151.1958750
          },
          "accuracy": 50,
          "name": "Google Shoes!",
          "types": ["shoe_store"],
          "language": "en-AU"
        }
    

    【讨论】:

    • 嗨 raj plz 阅读我的问题,我可以搜索地点,但现在我想添加地点。如果你知道什么,请与我分享。
    • 嗨 suraj,看到这个新答案。它会帮助你。
    【解决方案3】:
      NSString *str1 = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><PlaceAddRequest><location><lat>24.003372</lat><lng>75.770232</lng></location><accuracy>50</accuracy><name>test pet house</name><type>pet_store</type><language>en-US</language></PlaceAddRequest>"];
      NSLog(@"str1=====%@",str1);
    
    NSString *str2 = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSData *requestdata = [NSData dataWithBytes:[str2 UTF8String] length:[str2 length]];
    NSString *postLength = [NSString stringWithFormat:@"%d", [requestdata length]];
    
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/add/xml?sensor=false&key=your own api key"]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[NSData dataWithBytes:[str1 UTF8String] length:[str1 length]]];
    
    //NSURLConnection *placesConn =[[NSURLConnection alloc] initWithRequest:request delegate:self];
    NSData *returndata = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnstr = [[[NSString alloc] initWithData:returndata encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"returnstr: %@",returnstr);
    

    使用上面的代码你肯定会为 google api 添加新的地方......只需在上面的代码中更改你的 lat、long、name 和你的 api 键值..

    【讨论】:

    • 嘿,我使用此代码并将所有值替换为我自己的值,并在响应中收到 ok。问题是这个地方仍然没有显示在我的地图上。我正在使用相同的 API 密钥和附近的搜索。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 2012-12-23
    • 2015-09-19
    • 1970-01-01
    • 2012-04-27
    相关资源
    最近更新 更多