java持续时间,时间返回nulldistancematrix.api
java Duration, time returning null distancematrix.api
我对如何使用 google 获取持续时间、时间和行进距离感到困惑 distancematrix.api 我不明白我的代码有什么问题,我得到的都是空值.为什么 ??下面是我的 javacode
DistanceMatrix results1 = DistanceMatrixApi.getDistanceMatrix(context,
new String[] {"rose hill"}, new String[] {"port louis"}).units(Unit.METRIC).await();
System.out.println(results1.rows[0].elements[0].duration);
System.out.println(results1.rows[0].elements[0].distance);
System.out.println(results1.rows[0].elements[0].distance);
请求的上下文
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIzaSyC..")
.build();
它对我有用,只需在 google 地图上创建(并启用)一个 API 键
这是我的代码
public class GoogleDistance {
public static void main(String[] args) throws InterruptedException, ApiException, IOException {
new GoogleDistance().go();
}
void go() throws InterruptedException, ApiException, IOException {
String API_KEY = "AIzy....";
GeoApiContext.Builder builder = new GeoApiContext.Builder();
builder.apiKey(API_KEY);
GeoApiContext geoApiContext = builder.build();
DistanceMatrix results1 = DistanceMatrixApi.getDistanceMatrix(geoApiContext,
new String[]{"rose hill"}, new String[]{"port louis"}).units(Unit.METRIC).await();
System.out.println(Arrays.toString(results1.destinationAddresses));
}
}
我认为问题可能出在您的打印件上:我认为您不应该这样做 rows[0].elements[0]
只需使用 Arrays.toString()。
当我 运行 我的代码输出 [Port Louis, Mauritius]
编辑
我想我现在明白你想做什么了。请看下面
public class GoogleDistance {
public static void main(String[] args) throws InterruptedException, ApiException, IOException {
new GoogleDistance().go();
}
void go() throws InterruptedException, ApiException, IOException {
String API_KEY = "AIzy...";
GeoApiContext.Builder builder = new GeoApiContext.Builder();
builder.apiKey(API_KEY);
GeoApiContext geoApiContext = builder.build();
DistanceMatrix results1 = DistanceMatrixApi.getDistanceMatrix(geoApiContext,
new String[]{"los angeles"}, new String[]{"san francisco"}).units(Unit.METRIC).await();
DistanceMatrixRow[] rows = results1.rows;
for (DistanceMatrixRow row : rows) {
DistanceMatrixElement[] elements = row.elements;
for (DistanceMatrixElement element : elements) {
Distance distance = element.distance;
if(distance == null){
System.out.println("distance is null");
continue;
}
String dist = distance.humanReadable;
String dur = element.duration.humanReadable;
System.out.println(dist);
System.out.println(dur);
}
}
}
}
我相信您得到的是空值,因为 google 无法在您提供的位置之间映射。
我对如何使用 google 获取持续时间、时间和行进距离感到困惑 distancematrix.api 我不明白我的代码有什么问题,我得到的都是空值.为什么 ??下面是我的 javacode
DistanceMatrix results1 = DistanceMatrixApi.getDistanceMatrix(context,
new String[] {"rose hill"}, new String[] {"port louis"}).units(Unit.METRIC).await();
System.out.println(results1.rows[0].elements[0].duration);
System.out.println(results1.rows[0].elements[0].distance);
System.out.println(results1.rows[0].elements[0].distance);
请求的上下文
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIzaSyC..")
.build();
它对我有用,只需在 google 地图上创建(并启用)一个 API 键
这是我的代码
public class GoogleDistance {
public static void main(String[] args) throws InterruptedException, ApiException, IOException {
new GoogleDistance().go();
}
void go() throws InterruptedException, ApiException, IOException {
String API_KEY = "AIzy....";
GeoApiContext.Builder builder = new GeoApiContext.Builder();
builder.apiKey(API_KEY);
GeoApiContext geoApiContext = builder.build();
DistanceMatrix results1 = DistanceMatrixApi.getDistanceMatrix(geoApiContext,
new String[]{"rose hill"}, new String[]{"port louis"}).units(Unit.METRIC).await();
System.out.println(Arrays.toString(results1.destinationAddresses));
}
}
我认为问题可能出在您的打印件上:我认为您不应该这样做 rows[0].elements[0]
只需使用 Arrays.toString()。
当我 运行 我的代码输出 [Port Louis, Mauritius]
编辑
我想我现在明白你想做什么了。请看下面
public class GoogleDistance {
public static void main(String[] args) throws InterruptedException, ApiException, IOException {
new GoogleDistance().go();
}
void go() throws InterruptedException, ApiException, IOException {
String API_KEY = "AIzy...";
GeoApiContext.Builder builder = new GeoApiContext.Builder();
builder.apiKey(API_KEY);
GeoApiContext geoApiContext = builder.build();
DistanceMatrix results1 = DistanceMatrixApi.getDistanceMatrix(geoApiContext,
new String[]{"los angeles"}, new String[]{"san francisco"}).units(Unit.METRIC).await();
DistanceMatrixRow[] rows = results1.rows;
for (DistanceMatrixRow row : rows) {
DistanceMatrixElement[] elements = row.elements;
for (DistanceMatrixElement element : elements) {
Distance distance = element.distance;
if(distance == null){
System.out.println("distance is null");
continue;
}
String dist = distance.humanReadable;
String dur = element.duration.humanReadable;
System.out.println(dist);
System.out.println(dur);
}
}
}
}
我相信您得到的是空值,因为 google 无法在您提供的位置之间映射。