java - Parse double value and set in TextView -
for parse data use retrofit (gson) library. have problems parsing double value 'position'. full json can see here below. parse string array 'words' great double value have nothing in textview. can wrong , need fix , missed?
thanks help!
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 double double value = double.parsedouble(mlatlng); //set marker map mgooglemap.addmarker(new markeroptions().position(value) .title("geolocation system") .snippet("your last current location 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; } }
to answer question google map, need separate out lat , lon, , create latlng object. think should this:
@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 , lon latlng latlon = new latlng(double.parsedouble(arrlatlng[0]), double.parsedouble(arrlatlng[1])); //create latlng object //convert string double //double value = double.parsedouble(mlatlng); wouldn't work, need both values individually //set marker map, use latlng object latlon mgooglemap.addmarker(new markeroptions().position(latlon) .title("geolocation system") .snippet("your last current location available!") .icon(bitmapdescriptorfactory.fromresource(r.drawable.icon_location))); cameraposition cameraposition = new cameraposition.builder() .target(latlon).zoom(12).build(); mgooglemap.animatecamera(cameraupdatefactory .newcameraposition(cameraposition)); }
for more detailed info on getting google maps api set up, see other answer here: cannot add map in android studio stated in google "getting started" page; android studio gives error