1

Google マップ V2 を使用しています。geocoder場所を見つけるために使用したいのですがgetFromLocationName()、null 値が返され、その理由がわかりませんでした。私のコードを見て、私に提案してください。どうすればよいですか?

EditText sLocation = (EditText) v.findViewById(R.id.editText1);
                String location = sLocation.getText().toString();

                List<android.location.Address> addressList= null;

                if (location != null || !location.equals("")) {
                    Geocoder geocoder = new Geocoder(getActivity().getApplicationContext());
                    try {
                        addressList = geocoder.getFromLocationName(location, 1);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                    android.location.Address address = addressList.get(0);
                    LatLng latlng = new LatLng(address.getLatitude(), address.getLatitude());
                    gMap.addMarker(new MarkerOptions().position(latlng).title("Marker"));
                    gMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));

                }
4

1 に答える 1

1

null 値 form を取得していgetFromLocationName()ました。なぜなら、私addressListは場所から「null」値を取得していたからです。EditTextinonClickメソッドを宣言すると、次のgetFromLocationName()ような正しい値が返されます。

sButton =(Button) v.findViewById(R.id.generalId);
sButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                EditText sLocation = (EditText)getActivity().findViewById(R.id.editText1);
                location = sLocation.getText().toString();

                List<android.location.Address> addressList= null;

                if (location != null || location.length()>0) {
                    Geocoder geocoder = new Geocoder(getActivity().getApplicationContext());
                    try {
                        addressList = geocoder.getFromLocationName(location, 1);

                    } catch (IOException e) {

                            e.printStackTrace();
                    }

                    android.location.Address address = addressList.get(0);
                    String locality = address.getLocality();
                    Toast.makeText(getActivity(), locality, Toast.LENGTH_LONG).show();
                    double latitude = address.getLatitude();
                    double longitude = address.getLongitude();
                    LatLng latLng = new LatLng(latitude, longitude);
                    gMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
                    gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

                }

            }
        });
于 2016-09-12T18:07:15.483 に答える