2017-04-08 80 views
1

我想在活動開始時縮放到用戶的當前位置。我有兩個java類。一個是MapActivity.java,另一個是MapFragment.java。我正試圖在MapFragment.java上完成這項工作。嘗試在活動開始時獲取當前位置

private Actvity activity; 
    private volatile GoogleMap googleMap; 
    GoogleApiClient mGoogleApiClient; 
    LocationRequest mLocationRequest; 

    @Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    activity = getActivity(); 
} 

MapFragment.java/onMapReady;

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

    mGoogleApiClient=new GoogleApiClient.Builder(activity) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(activity) 
      .addOnConnectionFailedListener(activity) 
      .build(); 

    mGoogleApiClient.connect(); 

} 

在這種onMapReady,.addConnectionCallbacks(活性)和.addOnConnectionFailedListener(活性)是給像在圖像中的警告;

enter image description here

在他們正在實施GoogleApiClient.ConnectionCallbacks和GoogleApiCleint.OnConnectionFailedListener不投在這之前我已經分析的事例。沒有實施,我無法獲得這些功能;

@Override 
public void onConnected(Bundle bundle) { 

    mLocationRequest=LocationRequest.create(); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

    mLocationRequest.setInterval(1000); 

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this); 

} 

@Override 
public void onConnectionSuspended(int i) { 

} 
@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 

} 

而且我還得到LocationListeners的方法;

@Override 
public void onLocationChanged(Location location) { 
    if(location == null){ 
     Toast.makeText(this,"Cant get current location",Toast.LENGTH_LONG).show(); 

    } 
    else{ 
     LatLng ll= new LatLng(location.getLatitude(),location.getLongitude()); 
     CameraUpdate update= CameraUpdateFactory.newLatLngZoom(ll,15); 
     mGoogleMap.animateCamera(update); 
    } 
} 

如果我在片段中手工實現它們,是否會導致任何問題?在片段活動中獲取當前位置的正確方法是什麼?

回答

1

如果我在片段中手工實現它們,是否會導致任何 問題?

不需要。您只需要執行ConnectionCallbacksOnConnectionFailedListener

在片段活動中獲取當前位置的正確方法是什麼?

public class MyFragment extends Fragment implements 
     ConnectionCallbacks, OnConnectionFailedListener { 

    // Create an instance of GoogleAPIClient. 
    if (mGoogleApiClient == null) { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(LocationServices.API) 
     .build(); 
    } 

    protected void onStart() { 
     mGoogleApiClient.connect(); 
     super.onStart(); 
    } 

    protected void onStop() { 
     mGoogleApiClient.disconnect(); 
     super.onStop(); 
    } 
    @Override 
    public void onConnected(Bundle connectionHint) { 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); // this is your location 
     if (mLastLocation != null) {    
     mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude())); 
     mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude())); 
     } 
    } 
} 

Reference

+0

感謝您的解釋! – dizel

0

我已實施的活動和位置監聽器片段和MapReady被稱爲片段來獲得當前位置。請參閱下面的代碼。

MainActivity.Java

public class MainActivity extends AppCompatActivity { 
    public static final String TAG = MainActivity.class.getSimpleName(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     setFragment(new MapFragment()); 
    } 
    // class for being re-used by several instances 
    protected void setFragment(Fragment fragment) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = 
       fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(android.R.id.content, fragment); 
     fragmentTransaction.commit(); 
    } 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
     <FrameLayout 
     android:id="@+id/frame_containerone" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <ImageView 
     android:id="@+id/imagenext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_margin="16dp" 
     /> 
</RelativeLayout> 

MapFragment.Java

public class MapFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     LocationListener, OnMapReadyCallback { 
    private static final String TAG = "MapFragment"; 
    MainActivity mActivity; 
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
    private GoogleMap mMap; // Might be null if Google Play services APK is not available. 
    SupportMapFragment mapFragment; 
    private GoogleApiClient mGoogleApiClient; 
    private LocationRequest mLocationRequest; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.map_fragment, null, true); 
     mActivity = ((MainActivity) getActivity()); 
     initViews(view); 
     return view; 
    } 
    private void initViews(View view) { 
     mGoogleApiClient = new GoogleApiClient.Builder(mActivity) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
     // Create the LocationRequest object 
     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1 * 1000); // 1 second, in milliseconds 
     mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 
    @Override 
    public void onMapReady(GoogleMap map) { 
     mMap = map; 
     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    } 
    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     if (ActivityCompat.checkSelfPermission(mActivity, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (location == null) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } else { 
      handleNewLocation(location); 
     } 
    } 
    private void handleNewLocation(Location location) { 
     Log.d(TAG, location.toString()); 
     double currentLatitude = location.getLatitude(); 
     double currentLongitude = location.getLongitude(); 
     LatLng latLng = new LatLng(currentLatitude, currentLongitude); 
     MarkerOptions options = new MarkerOptions() 
       .position(latLng) 
       .title("I am here!"); 
     mMap.addMarker(options); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    } 
    @Override 
    public void onConnectionSuspended(int i) { 
    } 
    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
     if (connectionResult.hasResolution()) { 
      try { 
       // Start an Activity that tries to resolve the error 
       connectionResult.startResolutionForResult(mActivity, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
       /* 
       * Thrown if Google Play services canceled the original 
       * PendingIntent 
       */ 
      } catch (IntentSender.SendIntentException e) { 
       // Log the error 
       e.printStackTrace(); 
      } 
     } else { 
      /* 
      * If no resolution is available, display a dialog to the 
      * user with the error. 
      */ 
      Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode()); 
     } 
    } 
    @Override 
    public void onLocationChanged(Location location) { 
     handleNewLocation(location); 
    } 
    @Override 
    public void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
     mGoogleApiClient.connect(); 
    } 
    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. 
      mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map); 
      mapFragment.getMapAsync(this);// mapFragment.getMapAsync((OnMapReadyCallback) mActivity); 
     } 
    } 
    @Override 
    public void onPause() { 
     super.onPause(); 
     if (mGoogleApiClient.isConnected()) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
      mGoogleApiClient.disconnect(); 
     } 
    } 
} 

map_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <!-- <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" />--> 
     <fragment 
      android:id="@+id/map" 
      android:name="com.google.android.gms.maps.SupportMapFragment" 
      android:layout_width="match_parent" 
      android:layout_height="388dp" 
      android:layout_weight="0.40" /> 
</LinearLayout> 

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.theme.fragment"> 
    <permission 
     android:name="com.theme.fragment.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 
    <uses-permission android:name="com.theme.fragment.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <!-- Required to show current location --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <!-- Goolge API Key --> 
     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="Your_Api key" /> 
    </application> 
</manifest>