Search on Google Map Sdk

∥☆過路亽.° 提交于 2019-12-25 02:59:16

问题


I need to implement the map view in my app to locate the required place. I had tried with the SVGeocoder concept.

[SVGeocoder geocode:searchfield.text
             completion:^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, NSError *error) {
}

But suppose I am trying to search any restaurent then the result is nil.

I was looking on Google map sdk but don't know how to do search functionality on GMSCameraPosition class.

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
                                                          longitude:longitude
                                                               zoom:5]; 

how to search with the address using google sdk.

Thanks in advance.


回答1:


If I understood it correctly, you need the location co-ordinates from a address string. Its Forward geo-coding. You can take a look at Google's free api for this: Link1

You will need a API key from your google account to access this api and there is way to select a free or business plan depending on your number of requests.

You need to use a CLLocation object for getting co-ordinates from your address. I wrote a similar function.

  CLLocation* temp_location=[[CLLocation alloc]init]; temp_location=[GeoCoding findAddressCordinates:sourceAddressTxtField.text];

  // Class GeoCoding to find Co-ordinates

#import <Foundation/Foundation.h>
@interface GeoCoding : NSObject  {

}

+(CLLocation*)findAddressCordinates:(NSString*)addressString;

@end

  #import "GeoCoding.h"
#import <CoreLocation/CLAvailability.h>

@implementation GeoCoding

+(CLLocation*)findAddressCordinates:(NSString*)addressString {
    CLLocation *location;
    NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true", addressString];
    url = [url stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

    NSURL *wurl = [NSURL URLWithString:url];
    NSData *data = [NSData dataWithContentsOfURL: wurl];

    // Fail to get data from server
    if (nil == data) {

        NSLog(@"Error: Fail to get data");
    }
    else{
        // Parse the json data
        NSError *error;
        NSDictionary *json = [NSJSONSerialization
                              JSONObjectWithData:data
                              options:kNilOptions
                              error:&error];

        // Check status of result
        NSString *resultStatus = [json valueForKey:@"status"];
        // If responce is valid
        if ( (nil == error) && [resultStatus isEqualToString:@"OK"] ) {
            NSDictionary *locationDict=[json objectForKey:@"results"] ;
            NSArray *temp_array=[locationDict valueForKey:@"geometry"];
            NSArray *temp_array2=[temp_array valueForKey:@"location"];
            NSEnumerator *enumerator = [temp_array2 objectEnumerator];
            id object;
              while ((object = [enumerator nextObject])) {
                 double latitude=[[object valueForKey:@"lat"] doubleValue];
                 double longitude=[[object valueForKey:@"lng"] doubleValue];
                 location=[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

                   NSLog(@"CLLocation lat  is  %f  -------------& long   %f",location.coordinate.latitude, location.coordinate.longitude);

             }
        }
   }

    return location;
}

@end

You can then use this co-ordinates in your Google Map to focus your camera position.



来源:https://stackoverflow.com/questions/23949326/search-on-google-map-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!