hansemannn / titanium-googlemaps

🗺 Use the Google Maps SDK in Titanium
Other
87 stars 26 forks source link

decodePolylinePoints not working/empty #80

Closed m1ga closed 7 years ago

m1ga commented 7 years ago
maps.getDirections({
        origin: 'Mountain View, CA',
        destination: 'San Francisco, CA',
        success: function(e) {
            console.log(e);
            console.log(e.routes[0].overview_polyline.points);
            console.log(maps.decodePolylinePoints(e.routes[0].overview_polyline.points));
            //mapView.addPolyline(maps.decodePolylinePoints(e.routes[0].overview_polyline));
        },
        error: function(e) {
            Ti.API.error('Error: ' + e.error);
        },
        // Optional
        waypoints: ['Cupertino, CA', 'via:Sunnyvale, CA'],
    });

output is:

{ geocoded_waypoints: 
   [ { place_id: 'ChIJiQHsW0m3j4ARm69rRkrUF3w',
       types: [Object],
       geocoder_status: 'OK' },
     { place_id: 'ChIJq3fTG1e0j4ARtHjho-E_TTk',
       types: [Object],
       geocoder_status: 'OK' },
     { place_id: 'ChIJO13QqUW2j4ARosN83Sb7jXY',
       types: [Object],
       geocoder_status: 'OK' },
     { place_id: 'ChIJIQBpAG2ahYAR_6128GcTUEo',
       types: [Object],
       geocoder_status: 'OK' } ],
  status: 'OK',
  routes: 
   [ { bounds: [Object],
       summary: 'CA-85 S',
       waypoint_order: [],
       legs: [Object],
       warnings: [],
       copyrights: 'Map data ©2017 Google',
       overview_polyline: [Object] } ] }
[Ti:INFO] u}dcFrnchVd@XXLHQnA_DlB_FfDkItF{NzM{\hEaNxAyEh@gBPo@J?NBn@X^JdAIb@YxByBz@W~A?n@BTKdADhB@r@AbBMxCk@~CoAzKyFtY}NjEiAhFa@t\L`EKjGu@lGyAzDsArGgCdGmBrE_A`Fi@xFUnl@CpOGrD[zDiA|DaBbEkC~CuCjIsItJ_KtFqFvE_DbEmBvDeAzCK~FUvGQ@yB?m@GaP@wb@E_r@?eD@o@_@?{F@wG@q]AuL?}I@}P@yrAN_NK{I?mHJkKKe\EsBRsCpA_CpDyBhFwB~EoApBwA~@yA\oG@YCFo@z@iIl@yDn@qDAMSY_G{AuFqAg@MHPHl@]`DuBxP{JcC{N_DoKwCuFs@gIaBeZcHePuDcGu@cHq@wFoAgTaFgX{Eq@oANoAb@_@f@?\TTf@@pAE`BoHpj@gDxVaHth@mGtd@}D~TaCpOsJpt@}AjKmA|E{@vCcMjb@wIhZ_EnL}EbJuKtPuQvXkRtYuJtNgU~V}U`Wux@h{@kb@ld@}JhKoMlNuFxIi^ht@g}Av_Dyt@tzAgGxMgDdKyA~FeD`PcArGkBdQeMhqB}A|O}ArKeBbJeFfRyQni@sCxJuBhJ{Khl@uBpIcAfDaB~DiEhH}Wl[a~@pfA_dA~lA_\x_@eNdOqa@b_@mn@`k@kz@~u@ib@p_@gDbDqKpLif@dj@an@|q@}l@xp@{IhKiAlBmAbDwAvGYzCU`HmAdxAO`WUbG_@bEo@zEoBrIwCbIiBvDei@v_Aym@ffAgp@njAgBlCsChD_ErDuBxAgFlC}FfBkq@dP_H~AyI~AwRnAkNdAsJjByCbAiKlEiFdBgDt@eGr@sI\wb@lBoUbAqEDcDOkHmAkNuF{DuBaBqAqEkFwHgNiH}LgDsEeFqFoWkVkFeE_JuF{JkEkI}BcEc@cBI_DDuN~Ach@dHilAhP_v@nKgMfByEtAoB~@aJzGgF~CwF|AsFx@eEv@aBZyMfDwM`EeQpEqGlByC|AuQ`IkDbBaJtEwGxCeCp@mDf@qEH}BM}AYgEcAiDeBoJgHmD{AaOaDcK{BcJoBeEc@mCEqERiKd@yCl@mCdBuFvHyAtAsCfAcCNuC]mCkA}A}@qCeAeC]gEQaB?a@Jq@JaH`@aAPw@b@}BtCkDhFkB|DGn@@l@\pAt@~ALt@WbISxUMpG_AvMc@`Mu@zCaAfB_A`AyCjBiA_BqDqEeBwCgA{A}@oASBoBoC
[Ti:INFO] []

decodePolylinePoints() is empty and will crash the app when uncomment the mapView.addPolyline(...)line

hansemannn commented 7 years ago

The proper access is e.routes[0].overview_polyline.points, please confirm.

m1ga commented 7 years ago

was missing the .pointsin the addPolyline call. Now I get at least an execption:

[ERROR] The application has crashed with an uncaught exception 'NSInvalidArgumentException'. [ERROR] Reason: [ERROR] -[NSArrayM polyline]: unrecognized selector sent to instance 0x608000054880 [ERROR] Stack trace: [ERROR] 0 CoreFoundation 0x0000000109efdd33 exceptionPreprocess + 147 [ERROR] 1 libobjc.A.dylib 0x00000001049cf21e objc_exception_throw + 48 [ERROR] 2 CoreFoundation 0x0000000109f6df04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 [ERROR] 3 CoreFoundation 0x0000000109e83005 forwarding + 1013 [ERROR] 4 CoreFoundation 0x0000000109e82b88 _CF_forwarding_prep_0 + 120 [ERROR] 5 test_maps 0x00000001027919ed 37-[TiGooglemapsViewProxy addPolyline:]_block_invoke_2 + 38 [ERROR] 6 libdispatch.dylib 0x000000010b71d808 _dispatch_call_block_and_release + 12 [ERROR] 7 libdispatch.dylib 0x000000010b73f12e _dispatch_client_callout + 8 [ERROR] 8 libdispatch.dylib 0x000000010b7264fb _dispatch_main_queue_callback_4CF + 1054 [ERROR] 9 CoreFoundation 0x0000000109ec1e49 CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 9 [ERROR] 10 CoreFoundation 0x0000000109e8737d CFRunLoopRun + 2205 [ERROR] 11 CoreFoundation 0x0000000109e86884 CFRunLoopRunSpecific + 420 [ERROR] 12 GraphicsServices 0x000000010d0b0a6f GSEventRunModal + 161 [ERROR] 13 UIKit 0x0000000106335c68 UIApplicationMain + 159 [ERROR] 14 test_maps 0x0000000101f26dd4 main + 100 [ERROR] 15 libdyld.dylib 0x000000010b78868d start + 1

hansemannn commented 7 years ago

You need to map the points to a polyline proxy before, you can make use the full polyline API:

mapView.addPolyline(maps.createPolyline({
    points: points   
}));

Not sure if the lat or lon is on the first array position ([lat, lon] vs. [lon, lat]), but I'm pretty sure you'll figure that out. Closing here.

m1ga commented 7 years ago

The problem is I can't get the decoded points. Not sure if I use it right. But the string from the example is not giving any output:

var points = maps.decodePolylinePoints('a}dcF~nchVPLXLHQhAsCDKzAyDPe@fAqC`@aAh@sARc@pCoHJUj@yAj@{AL]`@cAd@iAbAiCnC_HjAsCvAqDL_@l@mB`@sA^kAJ[h@aBPi@DSJWDMHSFS@GXaABIBI\\eAHW?ATy@HSPo@');
console.log(points);
 console.log(points.length);
 _.each(points,function(item){
      console.log(item);
 })

// [Ti:INFO] []
// [Ti:INFO] 0

Using mapbox/polyline shows me the array: https://jsfiddle.net/9w4g15vb/

hansemannn commented 7 years ago

Will reopen for further investigation, @yozef Can you help out?

yozef commented 7 years ago

Morning,

This works for me:

var Map = require('ti.googlemaps');
Map.setAPIKey("AIza_Your_key"); // make sure to enable in google console directions

var points = Map.decodePolylinePoints('a}dcF~nchVPLXLHQhAsCDKzAyDPe@fAqC`@aAh@sARc@pCoHJUj@yAj@{AL]`@cAd@iAbAiCnC_HjAsCvAqDL_@l@mB`@sA^kAJ[h@aBPi@DSJWDMHSFS@GXaABIBI\\eAHW?ATy@HSPo@');
Ti.API.info(points);

[INFO] :   (
[INFO] :           {
[INFO] :           latitude = "37.38593";
[INFO] :           longitude = "-122.08384";
[INFO] :       },
[INFO] :           {
[INFO] :           latitude = "37.38584";
[INFO] :           longitude = "-122.08391";
[INFO] :       },
...

Are you sure you are using the latest ti.googlemaps module version 3.2.0?

yozef commented 7 years ago

I think I know whats the issue:

Ti.API.info("points " + points);
Ti.API.info("typeof points: " + typeof points); // is Object and not an Array

Try: var pointsArr = [points] to make it into an array (or ideally the module should return as a Ti Array Obj.

Update 2:

var points = Map.decodePolylinePoints('a}dcF~nchVPLXLHQhAsCDKzAyDPe@fAqC`@aAh@sARc@pCoHJUj@yAj@{AL]`@cAd@iAbAiCnC_HjAsCvAqDL_@l@mB`@sA^kAJ[h@aBPi@DSJWDMHSFS@GXaABIBI\\eAHW?ATy@HSPo@');
Ti.API.info("points " + JSON.stringify(points));

Console:

[
{"longitude":-122.08384000000001,"latitude":37.38593},{"longitude":-122.08391,"latitude":37.38584},{"longitude":-122.08398000000001,"latitude":37.38571},{"longitude":-122.08389000000001,"latitude":37.38566},{"longitude":-122.08315,"latitude":37.385290000000005},{"longitude":-122.08309000000001,"latitude":37.38526},{"longitude":-122.08216000000002,"latitude":37.384800000000006},{"longitude":-122.08197000000001,"latitude":37.384710000000005},{"longitude":-122.08124000000001,"latitude":37.384350000000005},{"longitude":-122.08091,"latitude":37.38418},{"longitude":-122.08049000000001,"latitude":37.383970000000005},{"longitude":-122.08031000000001,"latitude":37.38387},{"longitude":-122.07879000000001,"latitude":37.383140000000004},{"longitude":-122.07868,"latitude":37.38308},{"longitude":-122.07823,"latitude":37.38286},{"longitude":-122.07777000000002,"latitude":37.38264},{"longitude":-122.07762000000001,"latitude":37.38257},{"longitude":-122.07728000000002,"latitude":37.382400000000004},{"longitude":-122.07691000000001,"latitude":37.38221},{"longitude":-122.07622,"latitude":37.381870000000006},{"longitude":-122.07478,"latitude":37.381150000000005},{"longitude":-122.07404000000001,"latitude":37.380770000000005},{"longitude":-122.07315000000001,"latitude":37.38033},{"longitude":-122.07299,"latitude":37.38026},{"longitude":-122.07244000000001,"latitude":37.380030000000005},{"longitude":-122.07202000000001,"latitude":37.37986},{"longitude":-122.07164000000002,"latitude":37.3797},{"longitude":-122.07150000000001,"latitude":37.37964},{"longitude":-122.07101000000002,"latitude":37.379430000000006},{"longitude":-122.0708,"latitude":37.379340000000006},{"longitude":-122.07070000000002,"latitude":37.379310000000004},{"longitude":-122.07058,"latitude":37.379250000000006},{"longitude":-122.07051000000001,"latitude":37.379220000000004},{"longitude":-122.07041000000001,"latitude":37.37917},{"longitude":-122.07031,"latitude":37.37913},{"longitude":-122.07027000000001,"latitude":37.37912},{"longitude":-122.06994000000002,"latitude":37.37899},{"longitude":-122.06989000000002,"latitude":37.37897},{"longitude":-122.06984000000001,"latitude":37.37895},{"longitude":-122.06949000000002,"latitude":37.378800000000005},{"longitude":-122.06937,"latitude":37.378750000000004},{"longitude":-122.06936,"latitude":37.378750000000004},{"longitude":-122.06907000000001,"latitude":37.378640000000004},{"longitude":-122.06897000000001,"latitude":37.37859},{"longitude":-122.06873000000002,"latitude":37.3785}
]

This is odd, because I think

Map.createPolyline({
    points: points   // is expecting [ [lat,lon], [lat,lon],... ]
})
yozef commented 7 years ago

I think I found it... in TiGooglemapsModule.m:

- (id)decodePolylinePoints:(id)args
{
    ENSURE_SINGLE_ARG(args, NSString);

    GMSPath *path = [GMSPath pathFromEncodedPath:args];
    NSMutableArray *coordinates = [NSMutableArray arrayWithCapacity:path.count];

    // returning as Array of objects {lat: xx.xx, long: yy.yy}, should be: [lat, lon]
    for (NSUInteger i = 0; i < path.count; i++) {
        CLLocationCoordinate2D location = [path coordinateAtIndex:i];
        [coordinates addObject:@{@"latitude": NUMDOUBLE(location.latitude), @"longitude": NUMDOUBLE(location.longitude)}];
    }

    return coordinates;
}
m1ga commented 7 years ago

@yozef I'm using 3.2.1 If I run

var points = maps.decodePolylinePoints('a}dcF~nchVPLXLHQhAsCDKzAyDPe@fAqC`@aAh@sARc@pCoHJUj@yAj@{AL]`@cAd@iAbAiCnC_HjAsCvAqDL_@l@mB`@sA^kAJ[h@aBPi@DSJWDMHSFS@GXaABIBI\\eAHW?ATy@HSPo@');
Ti.API.info("points " + JSON.stringify(points));

it works fine. But if I put that in the successcallback of the getDirections() it will just display points (no items, see first post).

yozef commented 7 years ago

I think you mean 3.2.0

You may need to enable directions for your Key in Google Console. Another note, if Google Maps doesn't find a path from origin to destination, you won't have anything in e.routes[0]

m1ga commented 7 years ago

This is so strange:

function decode(){
    var str = 'a}dcF~nchVPLXLHQhAsCDKzAyDPe@fAqC`@aAh@sARc@pCoHJUj@yAj@{AL]`@cAd@iAbAiCnC_HjAsCvAqDL_@l@mB`@sA^kAJ[h@aBPi@DSJWDMHSFS@GXaABIBI\\eAHW?ATy@HSPo@';
    var points = maps.decodePolylinePoints(str);
    Ti.API.info("points " + JSON.stringify(points));
}

btn.addEventListener('click', function() {
    decode();                                                   // works fine
    maps.getDirections({
        origin: 'Mountain View, CA',
        destination: 'San Francisco, CA',
        success: function(e){
            decode();                                             // points is empty
        },
        error: function(e) {
            Ti.API.error('Error: ' + e.error);
        }
    });
});

I'm not using any data from the callback, just trigger my function from there. Calling decode() from the click works, calling it inside the success callback: empty.

hansemannn commented 7 years ago

Working here, tried both main- and kroll-thread:

var maps = require('ti.googlemaps');

/**
 *  SET YOUR API-KEY BEFORE USING THIS MODULE
 */
maps.setApiKey('xxxxxx');

var win = Ti.UI.createWindow({
    backgroundColor: '#fff'
});

var mapView = maps.createView({
    region: {
        latitude: 37.368122,
        longitude: -121.913653
    }
});

var btn1 = Ti.UI.createButton({
    title: 'Get Directions!',
    top: 40,
    left: 30
});

btn1.addEventListener('click', function() {
    maps.getDirections({
        origin: 'Mountain View, CA',
        destination: 'San Francisco, CA',
        success: function(e) {
            Ti.API.info(maps.decodePolylinePoints(e.routes[0].overview_polyline.points));
        },
        error: function(e) {
            Ti.API.error('Error: ' + e.error);
        },
        waypoints: ['Cupertino, CA', 'via:Sunnyvale, CA'] // Optional
    });
});

win.add(mapView);
win.add(btn1);
win.open();
hansemannn commented 7 years ago

I pushed one commit that ensures the events are fired from the main-thread. That could fix your particular issue, give it a try!

m1ga commented 7 years ago

With the fix it's working fine! Thanks

Also when I set run-on-main-thread:false it is working with old version. Didn't think of testing that before. So now it is working on the main thread, too!

Tested:

hansemannn commented 7 years ago

Thx. Will publish 3.2.2 today and do some more background checks regarding threading.