我可以在活动中设置fragmentActivity地图吗?

时间:2016-06-29 08:17:37

标签: android google-maps

我正在开发一个Android应用程序,其中我想在我的活动中的特定位置使用地图,那么我如何将fragmentActivity i作为活动? 在这里你会发现我的fragmentActivity是java代码

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {

public static FragmentManager fragmentManager;
private GoogleMap mMap;
private LocationManager locationManager;
private boolean isGPSEnabled, isNetworkEnabled, isGPSTrackingEnabled;
private String provider_info;
private static String TAG = MapsActivity.class.getName();
private Location location;
private double latitude;
private double longitude;
// The minimum distance to change updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    // initialising the object of the FragmentManager. Here I'm passing getSupportFragmentManager(). You can pass getFragmentManager() if you are coding for Android 3.0 or above.
    fragmentManager = getSupportFragmentManager();

    getMyLocation();

}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    mMap.setMyLocationEnabled(true);
    LocationManager locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);


    return;
}


public void onLocationChanged(Location location) {

    //txtOutput.setText(location.toString());
    System.out.println(String.valueOf(location.getLatitude()));
    System.out.println(String.valueOf(location.getLongitude()));


    try {

        mMap.clear();
    } catch (Exception e) {
    }
    LatLng mypos = new LatLng(location.getLatitude(), location.getLongitude());

    //camera annimation
    CameraPosition camPos = new CameraPosition.Builder().target(mypos)
            .zoom(70)
            .bearing(45)
            .tilt(65)
            .build();

    CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos);

    mMap.animateCamera(camUpd3);
    mMap.addMarker(new MarkerOptions().position(mypos).title("here i am !!"));

    // mMap.moveCamera(CameraUpdateFactory.newLatLng(mypos));
    //map.moveCamera(CameraUpdateFactory.newLatLng(mypos));


}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

void getMyLocation(){


    // definition of the location manager

    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    //definition of the listenner
    LocationListener locationListener = new LocationListener() {
        // Called when a new location is found by the network location provider.
        public void onLocationChanged(Location location) {
            // setting the camera of the map to positionate the marker in the current position
            LatLng myLaLn = new LatLng(location.getLatitude(), location.getLongitude());

            CameraPosition camPos = new CameraPosition.Builder().target(myLaLn)
                    .zoom(15)
                    .bearing(45)
                    .tilt(70)
                    .build();

            CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos);

            mMap.animateCamera(camUpd3);

            //  setting the marker in the current position

            Marker marker = mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(location.getLatitude(), location.getLongitude()))
                    .title("My position")
                    .snippet("Population: 776733"));
            //toast to give you the latitude and longitude of  yoiu current position
            Toast toast = Toast.makeText(getApplicationContext(), "your current location is : longitude :"+location.getLongitude()+"latitude: "+location.getLatitude(), Toast.LENGTH_SHORT);
            toast.show();

        }

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

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
    };
    //set the frequency of updates
    locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    //check the permission

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Location location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

}

}

0 个答案:

没有答案