【问题标题】:How to save multiple location coordinates in your project?如何在项目中保存多个位置坐标?
【发布时间】: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


【解决方案1】:

使用Double.parseDouble() 提高效率

核心方法是 parseDouble() 专门用于解析 一个包含浮点值的字符串到 Double 对象中。休息 的方法,例如valueOf() 和构造函数使用 parseDouble() 内部。

并确保您传递的 pdlat 值是有效的双精度数,而不仅仅是普通字符

这个(Double.parseDouble())方法会抛出NullPointerException,如果你传递的字符串是null,如果String不包含一个有效的double值,那么NumberFormatException。包含字母字符。

只需调试Double pdlatitude = new Double(pdlat); 行即可确定。

【讨论】:

    【解决方案2】:

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

    你的价值:

    12.311135688.568645787
    

    应该有这样的1分:

    12.311135688568645787
    

    附言。如果解析失败,可以捕获异常,具体怎么做,看发生什么异常。

    【讨论】:

      猜你喜欢
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多