2015-08-24 288 views
0

我無法獲取我在Google地圖活動中的當前位置,我確定權限是在Android Manifest中實現的,但它仍然會改爲0,0我目前的位置。在模擬器上,位置首選項設置爲打開。在打開應用程序時自動獲取當前位置

這是我的主要活動:

import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class RainstormMap extends FragmentActivity { 

private GoogleMap mMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_rainstorm_map); 
    setUpMapIfNeeded(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    setUpMapIfNeeded(); 
} 


private void setUpMapIfNeeded() { 

    if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment)    getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 
      setUpMap(); 
     } 
    } 
} 

/** 
* This is where we can add markers or lines, add listeners or move the camera. In this case, we 
* just add a marker near Africa. 
* <p/> 
* This should only be called once and when we are sure that {@link #mMap} is not null. 
*/ 
private void setUpMap() { 
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
} 

回答

0

您需要implemente位置服務!

使用LocationListener的和GoogleApiClient接口

public class RainstormMap extends FragmentActivity implements OnMapReadyCallback, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     LocationListener { 

    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 1000; 

    /** 
    * Map Settings 
    */ 
    private static final int DEFAULT_ZOOM = 15; 
    private static final int LOCATION_INTERVAL = 1000; 

    /** 
    * Map Properties and Utilities 
    */ 
    private GoogleMap mMap; 
    private GoogleApiClient mApiClient; 
    private LocationRequest mLocationRequest; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_rainstorm_map); 

     mApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 

     // Create the LocationRequest object 
     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(LOCATION_INTERVAL)  // 10 seconds, in milliseconds 
       .setFastestInterval(LOCATION_INTERVAL); // 1 second, in milliseconds 

     // setup map 
     ((MapFragment) getFragmentManager().findFragmentById(R.id.googleMap)).getMapAsync(this); 
    } 

    /** 
    * When get User Geo point call this method to list places by location 
    * 
    * @param location 
    *   User current location 
    */ 
    private void handleNewLocation(Location location) { 
     // go to user location 
     mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
       new LatLng(location.getLatitude(), 
         location.getLongitude()), 
       DEFAULT_ZOOM 
     )); 

     mMap.addMarker(new MarkerOptions() 
       .title("Marker") 
       .position(new LatLng(
         location.getLatitude(), 
         location.getLongitude())); 


    } 

    /** 
    * --------- Google API Connection and get Map if is ready --------- 
    */ 

    @Override 
    public void onConnected(Bundle bundle) { 
     Location loc = LocationServices.FusedLocationApi.getLastLocation(mApiClient); 

     if (loc != null) { 
      handleNewLocation(loc); 
     } else { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mApiClient, mLocationRequest, this); 
     } 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     if (connectionResult.hasResolution()) { 
      try { 
       // Start an Activity that tries to resolve the error 
       connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
      } catch (IntentSender.SendIntentException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.i(Const.TAG, "Location services connection failed with code " + connectionResult.getErrorCode()); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     //TODO connection suspended implement 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     handleNewLocation(location); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     mMap.setMyLocationEnabled(true); 
    } 

    /** 
    * --------- Activity Life Cycle --------- 
    */ 

    @Override 
    public void onResume() { 
     super.onResume(); 
     mApiClient.connect(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     if (mApiClient.isConnected()) { 
      mApiClient.disconnect(); 
     } 
    } 
} 

activity_rainstorm_map.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/rootView"> 

    <fragment 
     android:id="@+id/googleMap" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:tag="ftag_google_map" 
     android:name="com.google.android.gms.maps.MapFragment"/> 

</RelativeLayout> 
+0

代碼編譯正確,謝謝。它只是不顯示我的位置仍然或標記。我確實得到了位置按鈕,但它不會去我的位置。 – SeePlusPlus

相關問題