Android Google Map Api Latlng String无法强制转换为Double

时间:2017-07-28 08:52:21

标签: android dictionary casting loopback

我正在使用node.js环回框架,并尝试获取医院的位置。 而且我一直在

 "java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double at " on  MarkerOptions options = new MarkerOptions()
                                .position(new LatLng(hospital.getGeolocation().get("lat"), hospital.getGeolocation().get("lng")))
                                .title(hospital.getName());

此错误即使似乎没有与投射相关的错误。我还确保了hospital.getGeolocation()。get(“lng”)返回double类型。我真的不知道问题是什么!请帮我!

floatingSearchView.setOnSearchListener(new FloatingSearchView.OnSearchListener() {
            @Override
            public void onSuggestionClicked(final SearchSuggestion searchSuggestion) {
                hospitalRepository.search(ImmutableMap.of("filter[where][name][like]", searchSuggestion.getBody()), new ListCallback<Hospital>() {
                    @Override
                    public void onSuccess(List<Hospital> hospitalObjects) {
                        Log.i("TAG", hospitalObjects.toString());
                        recyclerAdapter.swapData(hospitalObjects);
                        isMapMovedBySearch = true;
                        googleMap.clear();
                        LatLngBounds.Builder builder = new LatLngBounds.Builder();
                        for(Hospital hospital : hospitalObjects) {
                            Log.i("TAG", hospital.toString());
                            double lat = hospital.getGeolocation().get("lat").doubleValue();
                            double lng = hospital.getGeolocation().get("lng").doubleValue();

                            LatLng latLng1 = new LatLng(lat, lng);
                            MarkerOptions options = new MarkerOptions()
                                    .position(latLng1)
                                    .title(hospital.getName());
                            builder.include(googleMap.addMarker(options).getPosition());
                        }
                        LatLngBounds bounds = builder.build();
                        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 0);
                        googleMap.moveCamera(cu);
                    }
                    public void onError(Throwable t){
                        Log.e("error", "Not connected to loopback server!");
                        Log.e("error", "TRACE", t);
                    }
                });
                mLastQuery = searchSuggestion.getBody();


            }

            @Override
            public void onSearchAction(String query) {
                mLastQuery = query;
                hospitalRepository.search(ImmutableMap.of("filter[where][name][like]", query), new ListCallback<Hospital>() {
                    @Override
                    public void onSuccess(List<Hospital> hospitalObjects) {

                        isMapMovedBySearch = true;
                        googleMap.clear();
                        LatLngBounds.Builder builder = new LatLngBounds.Builder();

                        for(Hospital hospital : hospitalObjects) {
                            Log.i("TAG", "Lag :" + hospital.getGeolocation().get("lat") );
                            Log.i("TAG","lng :" + hospital.getGeolocation().get("lng"));
                            MarkerOptions options = new MarkerOptions()
                                    .position(new LatLng(hospital.getGeolocation().get("lat"), hospital.getGeolocation().get("lng")))
                                    .title(hospital.getName());
                            //builder.include(googleMap.addMarker(options).getPosition());
                            googleMap.addMarker(options);
                        }
                        recyclerAdapter.swapData(hospitalObjects);
                        LatLngBounds bounds = builder.build();
                        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 0);
                        googleMap.moveCamera(cu);
                    }

                    public void onError(Throwable t){
                        Log.e("error", "Not connected to loopback server!");
                        Log.e("error", "TRACE", t);
                    }
                });
            }
        });

logcat的

E/error: Not connected to loopback server!
E/error: TRACE
         java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
             at com.yeomiae.dev.redmom.Fragments.HospitalFragment$4$1.onSuccess(HospitalFragment.java:432)
             at com.strongloop.android.loopback.callbacks.JsonArrayParser.onSuccess(JsonArrayParser.java:31)
             at com.strongloop.android.remoting.adapters.Adapter$JsonArrayCallback.onSuccess(Adapter.java:138)
             at com.strongloop.android.remoting.adapters.Adapter$JsonCallback.onSuccess(Adapter.java:81)
             at com.strongloop.android.remoting.adapters.RestAdapter$CallbackHandler.onSuccess(RestAdapter.java:220)
             at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:276)
             at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:134)
             at android.os.Handler.dispatchMessage(Handler.java:102)
             at android.os.Looper.loop(Looper.java:135)
             at android.app.ActivityThread.main(ActivityThread.java:5430)
             at java.lang.reflect.Method.invoke(Native Method)
             at java.lang.reflect.Method.invoke(Method.java:372)
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)

2 个答案:

答案 0 :(得分:0)

试着这样说:

double lat = Double.parseDouble(hospital.getGeolocation().get("lat"));

答案 1 :(得分:0)

在Java中,您必须准确指定所需的类型。 因此,如果方法需要double并且您传递字符串,它将创建一个类强制转换异常。

试试这个:

Double.parseDouble("12.322")