2016-08-18 38 views
0

當我放大或縮小地圖後,某些第二地圖會自動轉至默認縮放位置或告訴是否有更好的方法來設置地圖以獲取當前位置。此外,我還想在我的片段中設置一個可滾動的頁腳任何人都有想法請幫助我。 這裏是地圖片段的代碼地圖在放大或縮小後進行了一些調整後更新

public class HomeFragment extends Fragment implements OnMapReadyCallback, 
     GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, 
     LocationListener { 

    private GoogleMap map; 
    private LocationRequest mLocationRequest; 
    private GoogleApiClient mGoogleApiClient; 
    private Location mLastLocation; 
    private Marker marker; 

    public static HomeFragment newInstance() { 
     HomeFragment fragment = new HomeFragment(); 
     return fragment; 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_home, null, false); 
     buildGoogleApiClient(); 
     mGoogleApiClient.connect(); 
     SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
     return view; 
    } 
    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap setMap) { 
     map = setMap; 
     setUpMap(); 
    } 
    public void setUpMap() { 
     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), 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; 
     } 
     map.setMyLocationEnabled(true); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     if (mGoogleApiClient != null) {  LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     } 
    } 

    protected synchronized void buildGoogleApiClient() { 
     Toast.makeText(getContext(), "buildGoogleApiClient", Toast.LENGTH_SHORT).show(); 
     mGoogleApiClient = new GoogleApiClient.Builder(getContext()) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 
    @Override 
    public void onConnected(Bundle bundle) { 
     Toast.makeText(getContext(), "onConnected", Toast.LENGTH_SHORT).show(); 

     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(1000); 
     mLocationRequest.setFastestInterval(1000); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
     //mLocationRequest.setSmallestDisplacement(0.1F); 
     if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), 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; 
     } 
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 
    @Override 
    public void onConnectionSuspended(int i) { Toast.makeText(getContext(),"onConnectionSuspended",Toast.LENGTH_SHORT).show(); 
    } 
    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     Toast.makeText(getContext(),"onConnectionFailed",Toast.LENGTH_SHORT).show(); 
    } 
    @Override 
    public void onLocationChanged(Location location) { 
     mLastLocation = location; 

     //remove previous current location Marker 
     if (marker != null){ 
      marker.remove(); 
     } 
     double dLatitude = mLastLocation.getLatitude(); 
     double dLongitude = mLastLocation.getLongitude(); 
     marker = map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude)) 
       .title("My Location").icon(BitmapDescriptorFactory 
         .defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 14)); 
    } 
    @Override 
    public void onAttach(Activity activity) { 
     //noinspection deprecation 
     super.onAttach(activity); 
    } 
    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 
} 

回答

0

onLocationChanged方法調用週期性是這樣的原因。刪除這一行。

map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 14));

@Override 
public void onLocationChanged(Location location) { 
    mLastLocation = location; 

    //remove previous current location Marker 
    if (marker != null){ 
     marker.remove(); 
    } 
    double dLatitude = mLastLocation.getLatitude(); 
    double dLongitude = mLastLocation.getLongitude(); 
    marker = map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude)) 
      .title("My Location").icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_RED))); 

} 
+0

刪除此線圖後不顯示我的當前位置和標記 –

+0

我希望在地圖上是開放的節目我用14縮放級別,如果你只想要第一次放大,然後使用onConnected方法 –

+0

當前位置因爲它只是第一次打電話。 –

相關問題