gpt4 book ai didi

android - 如何使用谷歌地图显示持续时间?

转载 作者:行者123 更新时间:2023-11-30 00:52:20 26 4
gpt4 key购买 nike

我正在尝试使用纬度和经度向用户显示两个位置之间的持续时间。我在权限方面遇到问题,我不确定如何处理。

这是我的 doInBackground 代码:

protected String[] doInBackground(String... params) {


try {
LocationManager locationManager = (LocationManager) MyCustomHomeActivity.this
.getSystemService(LOCATION_SERVICE);

// getting GPS status
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);

// getting network status
boolean isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {

// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {

if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}

} catch (Exception e) {
e.printStackTrace();
}


String shuLat = "41.2207188";
String shuLong = "-73.24168179999999";
String forecastJsonStr = null;

String myUrlSetup = "https://maps.googleapis.com/maps/api/directions/json?origin="+latitude + "," + longitude +"&destination="+shuLat +"," + shuLong + "&departure_time=now&traffic_model=best_guess&key=AIzaSyB6l8vrnspw2-1Q_cnzO03JlAsIOMl-7bs";
HttpURLConnection urlConnection = null;
BufferedReader reader = null;

try {

URL url;
url = new URL(myUrlSetup);


// Create the request to GoogleMapAPI, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();

// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
System.out.println("I am in doInBackground step3");
reader = new BufferedReader(new InputStreamReader(inputStream));

String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}

if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
forecastJsonStr = buffer.toString();

Log.v(LOG_TAG, "Forecast string: " + forecastJsonStr);
} catch (IOException e) {
Log.e(LOG_TAG, "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}

try {
System.out.println("I am just in front of calling getDurationDataFromJson");
return getDurationDataFromJson(forecastJsonStr);
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
return new String[0];
}//end doInBackground

这是我的 getDurationDataFromJSON 代码:

private String[] getDurationDataFromJson(String forecastJsonStr)
throws JSONException {

// These are the names of the JSON objects that need to be extracted.
final String OWM_ROUTES = "routes";
final String OWM_LEGS = "legs";
final String OWM_DURATION = "duration";
final String OWM_TEXT = "text";


String[] resultStrs = new String[0];
String duration;

JSONObject durationJson = new JSONObject(forecastJsonStr);

JSONArray routeArray = durationJson.getJSONArray(OWM_ROUTES);

JSONArray legArray = routeArray.getJSONObject(0).getJSONArray(OWM_LEGS);

//Duration
JSONObject durationObj = legArray.getJSONObject(0).getJSONObject(OWM_DURATION);

duration = durationObj.getString(OWM_TEXT);

resultStrs[0] = duration;

System.out.println("Duration is: " + duration);
for (String s : resultStrs) {
System.out.println("Duration entry: " + s);
Log.v(LOG_TAG, "Duration entry: " + s);
}
return resultStrs;

}

我面临的问题是 doInBackGround 代码中的以下部分:

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

错误是:

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available(with `check permission`) or explicitly handle a potential `SecurityException`.

我不确定我的方向是否正确。请指导我完成此操作。

最佳答案

对于 Android API 级别 (23),在运行时请求权限,这里有官方文档:

https://developer.android.com/training/permissions/requesting.html

基本上,您需要检查设备的 Android API 级别是否 >= 23,如果是,则请求所需的权限。尝试这样的事情:

if ( Build.VERSION.SDK_INT >= 23)
//Ask for needed permissions following the docs mentioned above
}

希望对您有所帮助!

关于android - 如何使用谷歌地图显示持续时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40791321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com