【问题标题】:Android: Not able to get current location in Google Map fragmentAndroid:无法在谷歌地图片段中获取当前位置
【发布时间】:2015-09-09 07:29:07
【问题描述】:
public class location_a extends Fragment {

    TextView location;
    MapView mMapView;
    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.location_a, container, false);

        location = (TextView) rootView.findViewById(R.id.add);

        location.setText("Plot -315\nNext to Taj Hotel\nNear layout\nBangalore - 5600092\nKarnataka");
        setUpMapIfNeeded();

        return rootView;
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            SupportMapFragment mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_fragment));
            //Toast.makeText(getActivity(), "come in" + mMap, Toast.LENGTH_SHORT).show();
            //mMap = fm.getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
            Toast.makeText(getActivity(), "come in", Toast.LENGTH_SHORT).show();
                setUpMap();
            }
        }
    }

    private void setUpMap() {


        mMap.addMarker(new MarkerOptions().position(new LatLng(12.9120074,77.64910951)).title("Winfo Global"));
        mMap.addMarker(new MarkerOptions().position(new LatLng(13.0120074,77.94910951)).title("xyz builder"));

        // Enable MyLocation Layer of Google Map
        mMap.setMyLocationEnabled(true);

        // Get LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

        // Create a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Get the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Get Current Location
        //  Location myLocation = locationManager.getLastKnownLocation(provider);

        Location myLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (myLocation!=null) {
            double longitude = myLocation.getLongitude();
            double latitude = myLocation.getLatitude();
            String locLat = String.valueOf(latitude) + "," + String.valueOf(longitude);
            // set map type
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

            // Get latitude of the current location
            /// double latitude = myLocation.getLatitude();

            // Get longitude of the current location
            ///double longitude = myLocation.getLongitude();

            // Create a LatLng object for the current location
            LatLng latLng = new LatLng(latitude, longitude);
            LatLng myCoordinates = new LatLng(latitude, longitude);
            CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(myCoordinates, 12);
            mMap.animateCamera(yourLocation);
            // Show the current location in Google Map
            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(myCoordinates)      // Sets the center of the map to LatLng (refer to previous snippet)
                    .zoom(17)                   // Sets the zoom
                    .bearing(90)                // Sets the orientation of the camera to east
                    .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                    .build();                 // Creates a CameraPosition from the builder
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
        // Zoom in the Google Map
        mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
        // mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located"));
    }

}

HERE mMap 始终显示为 NULL,因此它不会进入 if 状态

if (mMap != null) {Toast.makeText(getActivity(), "comein", Toast.LENGTH_SHORT).show();
                setUpMap();
            }

请帮我解决问题

【问题讨论】:

  • 改进的代码格式。

标签: android dictionary fragment


【解决方案1】:

如果 Google Play 服务 APK 在应用程序中不可用,它始终为空,请检查您的代码

【讨论】:

    【解决方案2】:

    试试下面的代码

    SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    GoogleMap mMap = mapFrag.getMap();
    

    Check here 获取完整源代码和示例

    编辑

    确保您已在布局中编写以下代码以从 id 获取地图片段。

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>
    

    【讨论】:

    • 我让你的代码闲置,运行后,在模拟器中显示不幸停止了..请你帮帮我
    • 能否请您在真实设备中检查一下......并发布您的日志以供应用程序停止。
    • 是的,我也检查了我的设备...同样不幸的是,应用程序已停止,这里我的问题是,在 Tab1、Tab2、Tab3 和 Tab4 等 Tabview 中...,在 Tab3 中我尝试集成地图位置,此处显示地图 ll,但未检测到当前位置。我的代码在上面请检查
    • 我需要你的 logcat 输出导致应用程序不幸停止的原因。
    • 进程:only.fo.DDROID,PID:23024 java.lang.NullPointerException:尝试调用虚拟方法'com.google.android.gms.maps.GoogleMap com.google.android.gms。 maps.SupportMapFragment.getMap()' 在 only.fo.DDROID.guests_user.projects_a.location_a.setUpMapIfNeeded(location_a.java:54) 的空对象引用上 only.fo.DDROID.guests_user.projects_a.location_a.onCreateView(location_a .java:42) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
    【解决方案3】:

    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 2012-11-26
      • 1970-01-01
      • 2013-05-05
      相关资源
      最近更新 更多