首页 > 解决方案 > 如何在项目中保存多个位置坐标?

问题描述

我正在从服务器获取一些位置,需要将它们指向谷歌地图中的标记,所以我所做的是我创建了一个位置模型并使用 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);
    }

这就是我在谷歌地图上发布我的坐标的方式

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",
        }

标签: javaandroidlocationmapsandroid-volley

解决方案


你的 PDlat 有 2 分。这对 Double 无效,因为 Double 有 1 分。

你的价值:

12.311135688.568645787

它应该有这样的1点:

12.311135688568645787

PS。你可以捕获异常如果值解析失败,那么做什么取决于发生什么异常。


推荐阅读