如何在Titanium Alloy应用程序中的iOS Google地图上显示路线

时间:2015-03-24 18:42:45

标签: ios google-maps titanium-alloy

是否有适用于Titanium Alloy的模块添加,可让您在iOS应用程序中显示Google地图上的路线?即我想显示一条标记的路​​线,显示一辆汽车如何从A到B。我知道如何在地图上添加线条,但不知道如何计算出路线,或者更好地自动完成。

我显然可以通过创建一个webview来做到这一点,但如果可能的话,我宁愿不这样做。

2 个答案:

答案 0 :(得分:1)

iOS地图工具包可以显示注释,然后可以包含路线等地点。

Alloy Controller: index.js

var MapModule = require('ti.map');
var opera = MapModule.createAnnotation({
    latitude: -33.8569,
    longitude: 151.2153,
    centerOffset: {x: 80, y: 25},
    image: 'SydneyOperaHouse.jpg',
    title: 'Sydney Opera House',
    subtitle: 'Sydney, New South Wales, Australia',
 // For eventing, use the Map View's click event
 // and monitor the clicksource property for 'rightButton'.    
    rightButton: Ti.UI.iPhone.SystemButton.CONTACT_ADD
});

var bridge = MapModule.createAnnotation({
    latitude: -33.852222,
    longitude: 151.210556,
    pincolor: MapModule.ANNOTATION_PURPLE,   
    title: 'Sydney Harbour Bridge',
    subtitle: 'Port Jackson',
 // For eventing, use the Map View's click event
 // and monitor the clicksource property for 'leftButton'.
    leftButton: Ti.UI.iPhone.SystemButton.INFO_DARK
});

var mapview = MapModule.createView({
    mapType: MapModule.NORMAL_TYPE,
    region: {latitude: -33.87365, longitude: 151.20689, latitudeDelta: 0.1,     longitudeDelta: 0.1 },
    annotations: [bridge,opera]
});
$.index.add(mapview);
$.index.open();

enter image description here

不要忘记将此模块包含在 Tiapp.xml 中。

<modules>
    <!-- Add this line to your modules section -->
    <module platform="iphone">ti.map</module>
</modules>

答案 1 :(得分:0)

您可以使用以下方法在Google地图上绘图 //在地图上绘制路径

- (无效)getDrivingDirection

{

[mapGoogleView clear];

cameraGoogleMap = [GMSCameraPosition cameraWithLatitude:[locationSourceLat floatValue] longitude:[locationSourceLong floatValue] zoom:15];
[mapGoogleView setCamera:cameraGoogleMap];

CLLocationCoordinate2D positionSource = CLLocationCoordinate2DMake([locationDestLat floatValue], [locationDestLong floatValue]);
GMSMarker *markerSource = [GMSMarker markerWithPosition:positionSource];
markerSource.map = mapGoogleView;

NSString *strDirection = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%@,%@&destination=%@,%@&sensor=false&key=AIzaSyCdq4_-Isxdkvn1LBx8aw7AvdQiWLEqqQ8",locationSourceLat,locationSourceLong,locationDestLat,locationDestLong];
NSLog(@"GOOGLE DRAW URL %@",strDirection);
NSURL *url = [[NSURL alloc]initWithString:[strDirection stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray* latestRoutes = [json objectForKey:@"routes"];
NSString *points=[[[latestRoutes objectAtIndex:0] objectForKey:@"overview_polyline"] objectForKey:@"points"];

@try
{
    NSArray *temp= [self decodePolyLine:[points mutableCopy]];
    GMSMutablePath *path = [GMSMutablePath path];

    for(int idx = 0; idx < [temp count]; idx++)
    {
        CLLocation *location=[temp objectAtIndex:idx];
        [path addCoordinate:location.coordinate];
    }
    // create the polyline based on the array of points.

    GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
    rectangle.strokeWidth = 5.0;
    rectangle.map = mapGoogleView;
}
@catch (NSException * e)
{
    // TODO show error
    NSLog(@"");
}
NSLog(@"%f",mapGoogleView.myLocation.coordinate.latitude);

}

//解码折线以绘制路径

- (NSMutableArray *)decodePolyLine:(NSMutableString *)编码 {     [encoded replaceOccurrencesOfString:@&#34; \\&#34; withString:@&#34; \&#34; options:NSLiteralSearch范围:NSMakeRange(0,[编码长度])];     NSInteger len = [编码长度];     NSInteger index = 0;     NSMutableArray * array = [[NSMutableArray alloc] init];     NSInteger lat = 0;     NSInteger lng = 0;     while(index&lt; len)     {         NSInteger b;         NSInteger shift = 0;         NSInteger结果= 0;         做{             b = [encoded characterAtIndex:index ++] - 63;             结果| =(b&amp; 0x1f)&lt;&lt;转移;             shift + = 5;         } while(b&gt; = 0x20);         NSInteger dlat =((结果&amp; 1)?〜(结果&gt;&gt; 1):(结果&gt;&gt; 1));         lat + = dlat;         shift = 0;         result = 0;         做{             b = [encoded characterAtIndex:index ++] - 63;             结果| =(b&amp; 0x1f)&lt;&lt;转移;             shift + = 5;         } while(b&gt; = 0x20);         NSInteger dlng =((结果&amp; 1)?〜(结果&gt;&gt; 1):(结果&gt;&gt; 1));         lng + = dlng;         NSNumber * latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];         NSNumber *经度= [[NSNumber alloc] initWithFloat:lng * 1e-5];         // printf(&#34; [%f,&#34;,[纬度doubleValue]);         // printf(&#34;%f]&#34;,[经度doubleValue]);         CLLocation * loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue]经度:[经度floatValue]];         [array addObject:loc];     }

return array;

}