Вопрос

I want to know about how to draw a map direction between two places with the help of google place API .

Please provide help how to do it.

Это было полезно?

Решение 2

Just a 4 Simple Steps To add a Polyline

  1. Create a GMSMutablePath object.
  2. Set the points in RoutePath with the addCoordinate: or addLatitude:longitude:.
  3. Create a new GMSPolyline object using the path as an argument.
  4. Set the map property of the GMSPolyline.

Code Snippet as above steps

GMSMutablePath *RoutePath = [GMSMutablePath path];
[RoutePath addCoordinate:CLLocationCoordinate2DMake(-46.85, 129.10)];
[RoutePath addCoordinate:CLLocationCoordinate2DMake(-46.70, 129.30)];

GMSPolyline *polyline = [GMSPolyline polylineWithPath:RoutePath];

polyline.map = YourMapViewObject;

For Removing and Adding Polyline in Ios with Google Map there is a Good Documentation Provided by Google Itself with following link

how-to-draw-a-map-direction-between-two-places-by-google-place-api

Hope it helps

Above thing will work if you using GoogleMap Sdk in IOS

if you want to draw polyline on IOs Map (MKMapView) then Go to this Sample Project...

https://github.com/kadirpekel/MapWithRoutes

Другие советы

For creating a map direction of two location, first you need corresponding 2 latitude and longitude, then you need to call calculateRoutesFrom. here you can pass latitude and longitude for two place….

-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
    NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];
    NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude];

    NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr];
    NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
    NSLog(@"api url: %@", apiUrl);
    NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSStringEncodingConversionAllowLossy error:Nil];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"points:\\\"([^\\\"]*)\\\"" options:0 error:NULL];
    NSTextCheckingResult *match = [regex firstMatchInString:apiResponse options:0 range:NSMakeRange(0, [apiResponse length])];
    NSString *encodedPoints = [apiResponse substringWithRange:[match rangeAtIndex:1]];
    //NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];

    return [self decodePolyLine:[encodedPoints mutableCopy]];
}

By using -(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded, you can get the several latitude and longitude between given two latitude and longitude.

-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                                options:NSLiteralSearch
                                  range:NSMakeRange(0, [encoded length])];
    NSInteger len = [encoded length];
    NSInteger index = 0;
    NSMutableArray *array = [[NSMutableArray alloc] init] ;
    NSInteger lat=0;
    NSInteger lng=0;
    while (index < len) {
        NSInteger b;
        NSInteger shift = 0;
        NSInteger result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5] ;
        NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5] ;

        Place * pp = [[Place alloc] init];
        pp.name = @"place name";
        pp.latitude = [latitude doubleValue];
        pp.longitude = [longitude doubleValue];



        PlaceMark* from = [[PlaceMark alloc] initWithPlace:pp] ;
        [annotations addObject:from];



        printf("[%f,", [latitude doubleValue]);
        printf("%f]", [longitude doubleValue]);
        CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] ;
        [array addObject:loc];
    }

    return array;
}

A array of latitude and longitude will be return from this method..

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top