Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在Google Maps API V2中显示当前位置,此地图位于其中一个滑动选项卡中_Android_Google Maps_Location_Pagerslidingtabstrip_Currentlocation - Fatal编程技术网

Android 在Google Maps API V2中显示当前位置,此地图位于其中一个滑动选项卡中

Android 在Google Maps API V2中显示当前位置,此地图位于其中一个滑动选项卡中,android,google-maps,location,pagerslidingtabstrip,currentlocation,Android,Google Maps,Location,Pagerslidingtabstrip,Currentlocation,我试着用滑动标签做这个应用,我用这个网站的图图做的 然后我想将地图添加到一个选项卡中,这个地图将在本教程中显示当前位置:我有一些问题。任何帮助都将不胜感激。提前谢谢。 这是我在表1中的代码: import android.app.Dialog; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import androi

我试着用滑动标签做这个应用,我用这个网站的图图做的 然后我想将地图添加到一个选项卡中,这个地图将在本教程中显示当前位置:我有一些问题。任何帮助都将不胜感激。提前谢谢。 这是我在表1中的代码:

    import android.app.Dialog;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Chronometer;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;


/**
 * Created by Admin on 6/26/2015.
 */
public class Tab1 extends Fragment implements LocationListener {

    GoogleMap googleMap;


    @Nullable
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_1, container, false);

        // Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

        // Showing status
        if(status!= ConnectionResult.SUCCESS){ // Google Play Services are not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();

        }else { // Google Play Services are available

            // Getting reference to the SupportMapFragment of activity_main.xml
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

            // Getting GoogleMap object from the fragment
            googleMap = fm.getMap();

            // Enabling MyLocation Layer of Google Map
            googleMap.setMyLocationEnabled(true);

            // Getting LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

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

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

            // Getting Current Location
            Location location = locationManager.getLastKnownLocation(provider);

            if(location!=null){
                onLocationChanged(location);
            }
            locationManager.requestLocationUpdates(provider, 20000, 0, this);
        }

        return v;
    }


    @Override
    public void onLocationChanged(Location location) {
        // Getting latitude of the current location
        double latitude = location.getLatitude();

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

        // Creating a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);

        // Showing the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        // Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}
这是我的错误:

  • 错误:(57,75)错误:找不到符号方法getBaseContext()

  • 错误:(63,75)错误:不兼容的类型:无法转换Tab1 活动

  • 错误:(69,58)错误:找不到符号方法 getSupportFragmentManager()
  • 错误:(78,82)错误:找不到 符号可变位置服务
  • 试试这个:

    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.MapView;
    import com.google.android.gms.maps.MapsInitializer;
    
    private MapView mMapView;
    private GoogleMap googleMap;
    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment, container, false);
    
            try {
                MapsInitializer.initialize(getActivity().getApplicationContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            mMapView = (MapView) v.findViewById(R.id.map_view);
            mMapView.onCreate(savedInstanceState);
            mMapView.onResume();
    
            googleMap = mMapView.getMap();
            googleMap.setMyLocationEnabled(true);
            googleMap.getUiSettings().setZoomControlsEnabled(true);
            googleMap.getUiSettings().setMapToolbarEnabled(true);
            googleMap.setBuildingsEnabled(true);
            initView(v);
    
            return v;
        }