简体   繁体   中英

How to save multiple location coordinates in your project?

I'm fetching some of the locations from the server and need to point them as markers in the google map, so what i did was i created a model of the locations and used the volley library to fetch the details from the server and saved them in a variable and post it in the map. But I'm getting an error as java.lang.NumberFormatException: multiple points . So need some assistance. This is my model

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;
}

This is my Activity where i'm retreiving the information from the server

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);
    }

And this is how i'm posting my coordiantes in Google maps

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");

And this is my JSON

  "res": [
        {
            "pdloc_latitude": "12.3111356",
            "pdloc_longitude": "76.6075989",
        },
        {
            "pdloc_latitude": "88.568645787",
            "pdloc_longitude": "75.54544454887",
        }

Use Double.parseDouble() for better efficiency

The core method is parseDouble() which is specially designed to parse a String containing floating point value into the Double object. Rest of the methods eg valueOf() and constructor uses parseDouble() internally.

and be sure that the pdlat value you are passing is a valid double number and not just plain characters

This( Double.parseDouble() ) method will throw NullPointerException if the string you are passing is null and NumberFormatException if String is not containing a valid double value eg containing alphabetic characters.

Just debug Double pdlatitude = new Double(pdlat); line to be sure.

Your PDlat have 2 points. That is invalid for Double since Double have 1 point.

Your value:

12.311135688.568645787

What it should has 1 point like this:

12.311135688568645787

PS. You can catch an exception If the value parsing is fail, and what to do is up to what exception occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM