在工作灯中检索Http Adapter中的Lat和Long

时间:2012-07-31 10:53:22

标签: javascript jquery ibm-mobilefirst

我试图使用http适配器检索lat和long。当我尝试调用该过程时,我将能够检索结果。但我想只检索lat而不是其他附加信息。我附上了我的代码。

function getGmapLatLng(pAddress) {
var input = {
method : 'get',
returnedContentType : 'json',
path : 'maps/api/geocode/json',
parameters : {
'address' : pAddress,
'sensor' : 'false' // hard-coded
}
}; 
return WL.Server.invokeHttp(input);
var type = typeof input; 
if ("object" == type) {
if (true == response) {

// Drill down into the response object.
var results = response;
var result = results[0];
var geometry = result;
var location = geometry;
} 
else {

return null;
}
} 
else {
return null;
}

}

任何人都可以纠正我错误的地方

1 个答案:

答案 0 :(得分:1)

这应该是你的适配器: googleMap.xml:

<?xml version="1.0" encoding="UTF-8"?>
<wl:adapter name="googleMap"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:wl="http://www.worklight.com/integration"
    xmlns:http="http://www.worklight.com/integration/http">
    <displayName>googleMap</displayName>
    <description>googleMap</description>
    <connectivity>
        <connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
            <protocol>http</protocol>
            <domain>maps.googleapis.com</domain>
            <port>80</port>
        </connectionPolicy>
        <loadConstraints maxConcurrentConnectionsPerNode="2" />
    </connectivity>
    <procedure name="getLangLat"/>
</wl:adapter>

googleMap-impl.js(我为了简单起见,我硬编码了地址):

function getLangLat() {
    var input = {
        method : 'get',
        returnedContentType : 'json',
        path : 'maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false'
    };
    return WL.Server.invokeHttp(input);
}

您的应用程序JavaScript:

// Worklight comes with the jQuery framework bundled inside. If you do not want to use it, please comment out the line below.
window.$ = WLJQ;

function wlCommonInit(){
    // Common initialization code goes here
    getLangLat();
}

function getLangLat() {
// Invocation data details
    var invocationData = {
        // Adapter to invoke
        adapter: 'googleMap',
        // Procedure to invoke
        procedure: 'getLangLat',
        parameters: []
    };

    // Invoke procedure
    WL.Client.invokeProcedure(invocationData, {
        // On success callback
        onSuccess : onSuccess,
        // On failure callback
        onFailure : onFail,
        // timeout
        timeout : 30000
    });

}

function onSuccess (results) {
    alert('Latitude: ' + results.invocationResult.results[0].geometry.location.lat);
    alert('Longitude: ' + results.invocationResult.results[0].geometry.location.lng);
}

function onFail (error) {
    WL.Logger.debug(error);
}