解析 double 值并在 TextView 中设置
Parse double value and set in TextView
对于解析数据,我使用 retrofit (gson) 库。我在解析 double 值 'position' 时遇到问题。完整的 JSON 您可以在下面看到。我解析字符串数组 'words' 很好,但具有双重值,我在 TextView 中什么也没有。谁能说出到底出了什么问题,我需要解决什么问题以及我遗漏了什么?
感谢您的帮助!
JSON
MainActivity.java
w3wNetworkFactory.w3wGetInstance().w3wGetApiCall().getPosition(w3w_API_KEY, new Words("gliders.inquest.hardly"), new Callback<PositionResponse>() {
@Override
public void success(PositionResponse positionResponse, Response response) {
position_w3w.setText(Arrays.toString(positionResponse.getPosition()).replaceAll("\[|\]", ""));
//Log.d("w3w location: ", Arrays.toString(positionResponse.getPosition()));
String mLatLng = Arrays.toString(positionResponse.getPosition()).replaceAll("\[|\]", "");
//Convert String to double
double value = Double.parseDouble(mLatLng);
//Set marker to map
mGoogleMap.addMarker(new MarkerOptions().position(value)
.title("Geolocation system")
.snippet("Your last current location which was available!")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));
}
@Override
public void failure(RetrofitError error) {
error.toString();
}
});
PositionResponse.java
public class PositionResponse {
private double[] position;
public double[] getPosition() {
return position;
}
}
尝试使用double
。 long
用于整数。在您的 JSON 带有浮点数的数字中。
要回答关于 Google 地图的问题,您需要分离纬度和经度,并创建一个 LatLng object。我认为应该是这样的:
@Override
public void success(PositionResponse positionResponse, Response response) {
position_w3w.setText(Arrays.toString(positionResponse.getPosition()).replaceAll("\[|\]", ""));
//Log.d("w3w location: ", Arrays.toString(positionResponse.getPosition()));
String mLatLng = Arrays.toString(positionResponse.getPosition()).replaceAll("\[|\]", "");
String[] arrLatLng = mLatLng.split(","); //separate out lat and lon
LatLng latLon = new LatLng(Double.parseDouble(arrLatLng[0]), Double.parseDouble(arrLatLng[1])); //create the LatLng object
//Convert String to double
//double value = Double.parseDouble(mLatLng); this wouldn't work, you need to do both values individually
//Set marker to map, use LatLng object latLon
mGoogleMap.addMarker(new MarkerOptions().position(latLon)
.title("Geolocation system")
.snippet("Your last current location which was available!")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLon).zoom(12).build();
mGoogleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
有关完全设置 Google 地图 API 的更多详细信息,请在此处查看我的其他答案:
对于解析数据,我使用 retrofit (gson) 库。我在解析 double 值 'position' 时遇到问题。完整的 JSON 您可以在下面看到。我解析字符串数组 'words' 很好,但具有双重值,我在 TextView 中什么也没有。谁能说出到底出了什么问题,我需要解决什么问题以及我遗漏了什么?
感谢您的帮助!
JSON
MainActivity.java
w3wNetworkFactory.w3wGetInstance().w3wGetApiCall().getPosition(w3w_API_KEY, new Words("gliders.inquest.hardly"), new Callback<PositionResponse>() {
@Override
public void success(PositionResponse positionResponse, Response response) {
position_w3w.setText(Arrays.toString(positionResponse.getPosition()).replaceAll("\[|\]", ""));
//Log.d("w3w location: ", Arrays.toString(positionResponse.getPosition()));
String mLatLng = Arrays.toString(positionResponse.getPosition()).replaceAll("\[|\]", "");
//Convert String to double
double value = Double.parseDouble(mLatLng);
//Set marker to map
mGoogleMap.addMarker(new MarkerOptions().position(value)
.title("Geolocation system")
.snippet("Your last current location which was available!")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));
}
@Override
public void failure(RetrofitError error) {
error.toString();
}
});
PositionResponse.java
public class PositionResponse {
private double[] position;
public double[] getPosition() {
return position;
}
}
尝试使用double
。 long
用于整数。在您的 JSON 带有浮点数的数字中。
要回答关于 Google 地图的问题,您需要分离纬度和经度,并创建一个 LatLng object。我认为应该是这样的:
@Override
public void success(PositionResponse positionResponse, Response response) {
position_w3w.setText(Arrays.toString(positionResponse.getPosition()).replaceAll("\[|\]", ""));
//Log.d("w3w location: ", Arrays.toString(positionResponse.getPosition()));
String mLatLng = Arrays.toString(positionResponse.getPosition()).replaceAll("\[|\]", "");
String[] arrLatLng = mLatLng.split(","); //separate out lat and lon
LatLng latLon = new LatLng(Double.parseDouble(arrLatLng[0]), Double.parseDouble(arrLatLng[1])); //create the LatLng object
//Convert String to double
//double value = Double.parseDouble(mLatLng); this wouldn't work, you need to do both values individually
//Set marker to map, use LatLng object latLon
mGoogleMap.addMarker(new MarkerOptions().position(latLon)
.title("Geolocation system")
.snippet("Your last current location which was available!")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLon).zoom(12).build();
mGoogleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
有关完全设置 Google 地图 API 的更多详细信息,请在此处查看我的其他答案: