首页 > 解决方案 > 路线 API 无法在物理设备上运行

问题描述

我在 Xamarin.Android 上编写了一个应用程序。在模拟器上调试时,Directions API 可以完美运行。但是在物理设备上测试时它不起作用。我想问题可能出在http请求中。有人熟悉这样的问题吗?

public async Task<string> GetDirectionJsonAsync(LatLng location, LatLng destLocation, string mapkey)
{
    // Origin of route
    string str_origin = "origin=" + location.Latitude.ToString() + "," + location.Longitude.ToString();

    // Destination of route
    string str_destination = "destination=" + destLocation.Latitude.ToString() + "," + destLocation.Longitude.ToString();

    // Mode
    string mode = "mode=driving";

    string parameters = str_origin + "&" + str_destination + "&" + mode + "&key=" + mapkey;

    // Output
    string output = "json";

    string url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;

    var handler = new HttpClientHandler();
    HttpClient client = new HttpClient(handler);
    string jsonString = await client.GetStringAsync(url);

    return jsonString;
}

标签: xamarin.androidhttprequestgoogle-directions-api

解决方案


推荐阅读