【发布时间】:2020-02-17 10:24:48
【问题描述】:
我正在从服务器获取一些位置,需要将它们作为标记点在谷歌地图中,所以我创建了一个位置模型并使用 volley 库从服务器获取详细信息并将它们保存在变量中并将其发布到地图中。但是我收到了一个错误 java.lang.NumberFormatException: multiple points。 所以需要一些帮助。 这是我的模型
public class LocationModel
private String pdLatitude;
private String pdLongitude;
public LocationModel(String pdLatitude, String pdLongitude){
this.pdLatitude = pdLatitude;
this.pdLongitude = pdLongitude; }
public String getPdLatitude() {
return pdLatitude;
}
public String getPdLongitude() {
return pdLongitude;
}
这是我从服务器检索信息的活动
private void findRoute(){
StringRequest request = new StringRequest(Request.Method.GET,
Constant.Route_URL + "/" + driverschoolname + "/" + driverid,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String pdlatitude = "";
String pdlongitude = "";
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("res");
for (int i=0; i<array.length(); i++){
JSONObject object = array.getJSONObject(i);
StudentsPickDropModel pickDropModel = new StudentsPickDropModel(
object.getString("pdloc_latitude"),
object.getString("pdloc_longitude")
);
pdlatitude += pickDropModel.getPdLatitude();
pdlongitude += pickDropModel.getPdLongitude();
Toast.makeText(StudentsPickDropActivity.this, pickDropModel.getPdName(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Activity.this, GetRoute.class);
intent.putExtra("pd_latitude", pdlatitude);
intent.putExtra("pd_longitude", pdlongitude);
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Activity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
}
这就是我在 Google 地图中发布我的坐标的方式
Bundle bundle = getIntent().getExtras();
schoollat = bundle.getString("school_lat");
schoollng = bundle.getString("school_lng");
pdlat = bundle.getString("pd_latitude");
pdlng = bundle.getString("pd_longitude");
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if (location == null){
requestNewLocationData();
} else {
currentLat = location.getLatitude();
currentLng = location.getLongitude();
System.out.println("Current Latitude: "+location.getLatitude());
System.out.println("Current Longitude: "+location.getLongitude());
}
}
});
Double clat = currentLat;
System.out.println("Current Latitude : "+clat);
Double schoollatitude = new Double(schoollat);
Double schoollongitude = new Double(schoollng);
System.out.println("School latitude : "+schoollatitude + ", School Longitude : "+schoollongitude);
Double pdlatitude = new Double(pdlat);
Double pdlongitude = new Double(pdlng);
getDirections = findViewById(R.id.getdirection);
getDirections.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new FetchURL(GetRoute.this).execute(getUrl(placeOne.getPosition(), placeTwo.getPosition(), "driving"), "driving");
}
});
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(this);
places = new MarkerOptions().position(new LatLng(pdlatitude, pdlongitude)).title("Office");
这是我的 JSON
"res": [
{
"pdloc_latitude": "12.3111356",
"pdloc_longitude": "76.6075989",
},
{
"pdloc_latitude": "88.568645787",
"pdloc_longitude": "75.54544454887",
}
【问题讨论】:
-
哪一行导致该异常?
-
双 pdlatitude = new Double(pdlat);
-
请登录您的 pdlat。这个值可能有问题。
-
它合并了两个纬度 -> PDlat : 12.311135688.568645787
标签: java android location maps android-volley