【问题标题】:Google map is returning wrong location by geocoder in android谷歌地图在 android 中通过地理编码器返回错误的位置
【发布时间】:2017-01-18 13:16:58
【问题描述】:

我正在使用谷歌地图,我想在其中搜索位置,但它返回给我错误的位置。我找不到它有什么问题,我需要建议。请有人告诉我该怎么办?

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();
                    LatLng latlng = new LatLng(address.getLatitude(), address.getLatitude());
                    gMap.addMarker(new MarkerOptions().position(latlng).title("Marker"));
                    gMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));

                }

【问题讨论】:

  • OK,但是和谷歌地图有关。我怎样才能在其中获得正确的位置?

标签: android google-maps google-maps-api-3 geocoding google-geocoder


【解决方案1】:

我在谷歌地图上的位置有误,因为我没有得到正确的纬度和经度。现在我通过下面的代码通过geocoder 获得正确的位置。

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

            hideSoftKeyboard(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));

            }

        }
    });

而不是

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();
                    LatLng latlng = new LatLng(address.getLatitude(), address.getLatitude());
                    gMap.addMarker(new MarkerOptions().position(latlng).title("Marker"));
                    gMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));

                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多