java - 无法获取 Google map fragment 中的当前位置

标签 java android google-maps android-fragments location

我试图获取 map fragment 中的当前位置,但由于 googleMap.setMyLocationEnabled(true); 行中的错误而崩溃。 我使用 Location 和 Latlang 来检测当前位置。

mapFragment.java

public class mapFragment extends Fragment {

    MapView mMapView;
    private GoogleMap googleMap;
    Location location;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // inflat and return the layout
        View v = inflater.inflate(R.layout.fragment_map, container,
                false);
        mMapView = (MapView) v.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume();// needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
        googleMap.setMyLocationEnabled(true);
        googleMap = mMapView.getMap();
        // latitude and longitude
        LatLng lat = new LatLng(location.getLatitude(), location.getLongitude());
        /*double latitude = 17.385044;
        double longitude = 78.486671;*/

        // create marker
        MarkerOptions marker = new MarkerOptions().position(
                lat).title("Hello Maps");

        // Changing marker icon
        marker.icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

        // adding marker
        googleMap.addMarker(marker);
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(lat).zoom(12).build();
        googleMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));

        // Perform any camera updates here
        return v;
    }


    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }
}

fragment_map.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

最佳答案

看起来您遇到了一些问题,但崩溃正在发生,因为您需要在调用 setMyLocationEnabled() 之前调用 getMap()

更改此:

googleMap.setMyLocationEnabled(true);
googleMap = mMapView.getMap();

...对此:

googleMap = mMapView.getMap();
googleMap.setMyLocationEnabled(true);

更新:我能够让这段代码正常工作。

获取当前位置应使用 Fused Location Provider API 来完成,但这超出了这个问题的范围。

此示例代码中的方法可能适用于较旧的设备,但在 4.4.4 上,由于 getMyLocation() 已被弃用,它会返回空位置。

但是,在进行空检查后, map 会正确显示,您可以单击位置按钮将 map 移动到您当前的位置。

        MapView mMapView;
        private GoogleMap googleMap;
        Location location;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // inflat and return the layout
            View v = inflater.inflate(R.layout.fragment_main, container,
                    false);
            mMapView = (MapView) v.findViewById(R.id.mapView);
            mMapView.onCreate(savedInstanceState);

            mMapView.onResume();// needed to get the map to display immediately

            try {
                MapsInitializer.initialize(getActivity().getApplicationContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
            googleMap = mMapView.getMap();
            googleMap.setMyLocationEnabled(true);

            location = googleMap.getMyLocation();

            if (location != null) {

                // latitude and longitude
                LatLng lat = new LatLng(location.getLatitude(), location.getLongitude());

                //double latitude = 17.385044;
                //double longitude = 78.486671;
                //LatLng lat = new LatLng(latitude,longitude);

                // create marker

                MarkerOptions marker = new MarkerOptions().position(
                        lat).title("Hello Maps");

                // Changing marker icon
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

                // adding marker
                googleMap.addMarker(marker);

                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(lat).zoom(12).build();


                googleMap.animateCamera(CameraUpdateFactory
                        .newCameraPosition(cameraPosition));

            }

            // Perform any camera updates here
            return v;
        }

关于java - 无法获取 Google map fragment 中的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29480717/

相关文章:

android - 创建无限循环android的最佳方法

google-maps - 在 Google map 向导中加载样式

google-maps - 如何告诉 KmlLayer 自定义 map 投影?

java - 有没有办法使用1以外的数字作为输入来继续程序?它似乎没有认识到我遇到的问题

java - 为什么我的自定义 SecurityManager 在第 16 次使用 Constructor.newInstance 创建对象时会导致异常?

Android LVL 开发者测试不工作

javascript - 在谷歌地图上查看全景图像

java - 由于与 guava 相关的问题,无法在 java 中运行 youtubedata api 2.0

java - 定期更新 RecyclerView 上的项目

android - 在 Fragment 中调用 findViewById 查找按钮返回 null