Android:无法获取Google Map片段中的当前位置

时间:2015-09-09 07:29:08

标签: android dictionary fragment

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 (mMap != null) {Toast.makeText(getActivity(), "comein", Toast.LENGTH_SHORT).show();
                setUpMap();
            }

请帮我解决问题

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如果Google Play服务APK在应用程序中不可用,它总是为空,请您查看代码