2016-07-05 8 views
0

react-native-geocoderパッケージを使用して、複数の住所をジオコードします。問題は、明らかに私のコードはバッチリクエストでいくつかの問題があります。ジオコード複数の住所とリアクト

なぜコードは個々のリクエストに対して機能し、バッチリクエストでは機能しないのですか?このエラーを回避する方法はありますか?

async getLocations(locations) { 
    Geocoder.fallbackToGoogle(GEOCODE_API_KEY); 
    this.setState({ 
    annotations: await Promise.all(locations.map(this.getLocation)) 
    }); 
} 

async getLocation(location) { 
    try { 
    let res = await Geocoder.geocodeAddress(location.address); 
    console.log("RESULT:", location.address, await res[0].position); 
    return (
     { 
     latitude: await res[0].position.lat, 
     longitude: await res[0].position.lng, 
     title: location.provider, 
     subtitle: location.address, 
     } 
    ); 
    } 
    catch(err) { 
    console.log("Error fetching geodata:", err); 
    } 
    return null; 
} 

結果(唯一の最後の要求が作業):

Error fetching geodata Error: geocodePosition failed(…) 
Error fetching geodata Error: geocodePosition failed(…) 
Error fetching geodata Error: geocodePosition failed(…) 
Error fetching geodata Error: geocodePosition failed(…) 
Error fetching geodata Error: geocodePosition failed(…) 
RESULT getLocationPosition Object {lat: 22.544028, lng: 19.124154} 
Possible Unhandled Promise Rejection (id: 0): 
Cannot read property 'id' of null 
TypeError: Cannot read property 'id' of null 
    at http://hostserver/index.ios.bundle?platform=ios&dev=true&hot=true:35937:11 
    at Array.map (native) 
    at Constructor.render (http://hostserver/index.ios.bundle?platform=ios&dev=true&hot=true:35928:38) 
    at Constructor.proxiedMethod [as render] (http://hostserver/index.ios.bundle?platform=ios&dev=true&hot=true:9036:22) 
    at ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext (http://hostserver/index.ios.bundle?platform=ios&dev=true&hot=true:22776:28) 
    at ReactCompositeComponentWrapper._renderValidatedComponent (http://hostserver/index.ios.bundle?platform=ios&dev=true&hot=true:22802:24) 
    at ReactCompositeComponentWrapper._updateRenderedComponent (http://hostserver/index.ios.bundle?platform=ios&dev=true&hot=true:22728:30) 
    at ReactCompositeComponentWrapper._performComponentUpdate (http://hostserver/index.ios.bundle?platform=ios&dev=true&hot=true:22708:6) 
    at ReactCompositeComponentWrapper.updateComponent (http://hostserver/index.ios.bundle?platform=ios&dev=true&hot=true:22627:6) 
    at ReactCompositeComponentWrapper.receiveComponent (http://hostserver/index.ios.bundle?platform=ios&dev=true&hot=true:22527:6) 
+1

のために働くの貼り付けられたログは、私が考えている唯一の理由は、ジオコーダーが呼び出しを抑制する必要があるということです。上記のコードでは、呼び出しは可能な限り高速に実行されるようにキューに入れられます。これが本当の場合(キャッチのエラーの詳細を確認することで確認できます)、単にAPI呼び出しのデバウンスを解除することができます。あるいは、代わりにシーケンスを使用してください。 – hazardous

+0

公式の文書https://github.com/devfd/react-native-geocoderの最後の数行では、iOS上で実行される順番が必要です。 Gooogle APIのフォールバックを使用してAndroidでこれを行っている場合は、間違いなく絞り込みを開始しますが、それほど速くはありません。 – hazardous

+0

また、getLocationはjsonではなくPromiseを返すべきですか? – hazardous

答えて

1

にコメントで示唆したように、ジオコードAPIは、同時要求をサポートしていない可能性があります。

async getLocations(locations) { 
    Geocoder.fallbackToGoogle(GEOCODE_API_KEY); 
    const annotations = [] 
    for (const l of locations) { 
    const r = await this.getLocation(l) 
    if (r == null) continue; // or display error message or whatever 
    annotations.push(r) 
    this.setState({annotations}); // move this after the loop if you want only one update 
    } 
} 
0

バッチジオコーディングが組み込まれたソリューションを希望する場合は、SmartyStreets US Street Address APIのようなサービスを試すことができます。現在、そこに国際住所のAPIもあるが、それは内蔵のバッチを提供していません。あなたはそれについてhere

免責事項詳細を読むことができます:。実際のエラーの詳細がで利用可能ではないので、私はSmartyStreets

関連する問題