试图在我的谷歌地图上获取当前位置

时间:2017-06-01 15:50:54

标签: android google-maps

我试图在谷歌地图上获取我当前的位置,但它给了我一张空白地图。

这是我的片段

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationSource.OnLocationChangedListener, GoogleMap.OnMapClickListener {

    private GoogleMap mMap;
    private LatLng userLocation;
    private GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;
    public LocationManager mLocationManager;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);


  }

  @Override
  public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  }

  @Override
  public void onLocationChanged(Location location) {
    userLocation = new LatLng(location.getAltitude(), location.getLongitude());
    Toast.makeText(this, "found you...", Toast.LENGTH_SHORT).show();
    MarkerOptions markerOptions= new MarkerOptions();

    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 10));
    mMap.addMarker(new MarkerOptions().position(userLocation).title("you are here"));
  }


  @Override
  public void onMapClick(LatLng latLng) {

  }
}
然后那是我的清单

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="25" />

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="AIzaSyBuSE2O0siczDYt3oh3FVUgcO_MZs4-8FQ" />

<!-- To access Google+ APIs: -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.example.liadiluz.finalproject.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:name="android.support.multidex.MultiDexApplication"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Activities.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".services.MyIntentService"
        android:exported="false" />

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".Activities.MapsActivity"
        android:label="@string/title_activity_maps2"></activity>
</application>

这就是布局文件

 <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    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="match_parent"
    tools:context="com.example.liadiluz.finalproject.Activities.MapsActivity" />

    </RelativeLayout>

0 个答案:

没有答案