![](/img/trans.png)
[英]Ionic - App works fine on browser and iOS simulator but not in android simulator
[英]Android emulator not processing http get request, but iOS simulator works fine
我想要做的是:使用谷歌地圖 api 獲取數據,然后將數據傳遞到下一個屏幕。
有什么問題:它在 iOS 模擬器上運行良好,它設法進入下一個屏幕並用數據填充地圖(打印位置、“yay”、http 狀態代碼和“loopyays”)。 但是在 android 模擬器上,它只打印位置和“yay”,並掛在那里,不會導航到下一個屏幕。
什么地方出了錯? :/ 請幫幫我..
class _LoadingScreenState extends State<LoadingScreen> {
void getUserLocation() async {
try {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print(position);
Set<Marker> markers = {};
String url =
'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${position.latitude},${position.longitude}&maxprice=4&radius=1500&type=restaurant&keyword=cruise&key=AIzaSyDVzxxxxxxxxxxxxxxxxxxx';
print('yay');
http.Response data = await http.get(url);
print(data.statusCode);
var resultList = jsonDecode(data.body)['results'];
for (Map i in resultList) {
var coords = (i['geometry']['location']);
print('loopyay');
markers.add(Marker(
markerId: MarkerId(i['name']),
position: LatLng(coords['lat'], coords['lng'])));
}
Navigator.push(context, MaterialPageRoute(builder: (context) {
return UserLocationScreen(
userLocation: position,
markers: markers,
);
}));
} catch (e) {
print(e);
}
}
我的配置
androidmapsdk
< meta-data android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyDVzbjC2hViOnxxxxxxxxxx"/>
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.google.gms:google-services:4.2.0'
}
android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536M
在 Android 9 pie 中,默認不允許 http 請求。
解決方案:請在您的AndroidManifest.xml
文件中更新它,如下所示。
<application
...
android:usesCleartextTraffic="true">
... // Your current code.
</application>
祝你有美好的一天。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.