sexta-feira, 17 de fevereiro de 2012

Traçando rotas com no iOS com MKMapView - Teaser

Essa semana eu tive que dar um jeito de desenhar a rota no map em um aplicativo pra iOS. Eis o que foi preciso:
  • Estudar http://code.google.com/apis/maps/documentation/directions/
  • Desenhar a rota propriamente dita. Usei http://spitzkoff.com/craig/?p=108
  • E o seguinte codigo
  • -(NSArray*)decodePolyline:(NSString*)polyline{
    • if (!polyline || [polyline length] == 0) return nil;
    • const char* polylinechars = [polyline cStringUsingEncoding:NSUTF8StringEncoding];
    • int index = 0;
    • NSMutableArray* points = [[NSMutableArray alloc] init];
    • int currentLat = 0;
    • int currentLng = 0;
    • int next5bits;
    • int sum;
    • int shifter;
    • while (index < [polyline length]) {
      • sum = 0;
      • shifter = 0;
      • do {
        • next5bits = (int)polylinechars[index++] - 63;
        • sum |= (next5bits & 31) << shifter;
        • shifter += 5;
        } while (next5bits >= 32 && index <[polyline length]);
      • if (index >= [polyline length]) break;
      • currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
      • //calculate next longitude
      • sum = 0;
      • shifter = 0;
      • do {
        • next5bits = (int)polylinechars[index++] - 63;
        • sum |= (next5bits & 31) << shifter;
        • shifter += 5;
        } while (next5bits >= 32 && index < [polyline length]);
      • if (index >= [polyline length] && next5bits >= 32) break;
      • currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
      • CLLocation* location = [[CLLocation alloc ] initWithLatitude:(double)currentLat / 100000.0 longitude:(double)currentLng / 100000.0];
      • [points addObject:location];
      }
    • return points;
    }
Combinando essas informações já é possivel criar uma rota no mapa.

[Editado]
Projeto completo Aqui.

Nenhum comentário:

Postar um comentário