ReactNavtive架構教程(6)(完)

來源:互聯網
上載者:User

標籤:ios   移動開發   

原文:http://www.raywenderlich.com/99473/introducing-react-native-building-apps-javascript

注意:所有圖片放在了百度相簿空間,如果你看不到圖片,請複製圖片URL,然後粘貼到地址欄中進行查看。
根據位置尋找

在Xcode中開啟 Info.plist ,右鍵,Add Row,增加一個key。 使用NSLocationWhenInUseUsageDescription 作為鍵名,使用
PropertyFinder would like to use your location to find nearby properties作為索引值。

這個key用於提示使用者允許App使用他們的當前位置。
開啟 SearchPage.js, 找到TouchableHighlight 中渲染 ‘Location’ 按鈕的代碼,加入下列屬性值:

onPress={this.onLocationPressed.bind(this)}

這樣,當點擊Location按鈕,會調用 onLocationPressed 方法。
在SearchPage 類中,增加方法:

onLocationPressed() {  navigator.geolocation.getCurrentPosition(    location => {      var search = location.coords.latitude + ‘,‘ + location.coords.longitude;      this.setState({ searchString: search });      var query = urlForQueryAndPage(‘centre_point‘, search, 1);      this._executeQuery(query);    },    error => {      this.setState({        message: ‘There was a problem with obtaining your location: ‘ + error      });    });}

navigator.geolocation可擷取當前位置。這個方法定義在 Web API中,這對於曾經在瀏覽器中使用過位置服務的人來說並不陌生。React Native 架構用本地iOS服務重新實現了這個API。
如果當前位置擷取成功,我們將調用第一個箭頭函數,否則調用第二個箭頭函數,我們簡單顯示錯誤資訊。
因為我們修改了plist檔案,因此我們需要重新啟動App,而不能僅僅是Cmd+R了。請在Xcode中終止App,然後重新編譯運行App。
在使用基於地理定位的搜尋之前,我們需要指定一個Nestoria資料庫中的預設位置。在模擬器菜單中,選擇Debug\Location\Custom Location … 然後輸入 55.02的緯度和-1.42的經度。這是一個位於英格蘭北部的海邊小鎮。

Ray注:
在我們測試過程中,地理定位尋找有時有效,有時無效,有時候我們授權App使用地理定位後仍然會報許可權錯誤。我們不知道這是為什麼,可能是React
Native自身的問題吧!如果讀者遇到同樣的問題並找出原因,請告知我們。

唉,儘管這裡的房屋價格不像倫敦那麼貴—但也不是我所能承受的:]

ReactNavtive架構教程(6)(完)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.