Google地图当前位置未显示

时间:2017-02-25 18:25:10

标签: android google-maps

我有一个小问题。我正在谷歌地图上工作,我无法在谷歌地图上显示当前位置。谷歌地图始终显示非洲,而不是我的位置。

我正在使用我的三星S5和android version 6.X

这是我的活动:

首先,这些是我的导入

    import android.Manifest;
    import android.content.pm.PackageManager;
    import android.location.Geocoder;
    import android.location.Location;
    import android.os.Build;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.location.LocationListener;
    import com.google.android.gms.location.LocationRequest;
    import com.google.android.gms.location.LocationServices;
    import com.google.android.gms.maps.CameraUpdate;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.MapFragment;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.model.LatLng;
    import java.io.IOException;
    import java.util.List;

这是我的活动

    public class AddfavouritesActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {


GoogleMap mgoogleMap;
GoogleApiClient mGoogleApiClient;

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

    initMap();
    Button bt1 = (Button) findViewById(R.id.addFavoritesplace_valider);
    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getLocate(v);
        }
    });
}

private void initMap() {

    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map_fragment_add_favourites);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {

    mgoogleMap = googleMap;
    //goTolocationZoom(36.815050, 10.180392,15f);

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


}

private void goTolocation(double lat, double lng) {
    LatLng ll = new LatLng(lat, lng);
    CameraUpdate update = CameraUpdateFactory.newLatLng(ll);
    mgoogleMap.moveCamera(update);
}

private void goTolocationZoom(double lat, double lng, float zoom) {
    LatLng ll = new LatLng(lat, lng);
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
    mgoogleMap.moveCamera(update);

}

public void getLocate(View view) {
    EditText et = (EditText) findViewById(R.id.addfavorites_location_address);
    String location = et.getText().toString();


    Geocoder gc = new Geocoder(this);
    List<android.location.Address> list = null;
    try {
        list = gc.getFromLocationName(location, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    android.location.Address address = list.get(0);
    String locality = address.getLocality();

    Toast.makeText(this, locality, Toast.LENGTH_LONG).show();
    double lat = address.getLatitude();
    double lng = address.getLongitude();
    goTolocationZoom(lat, lng, 15);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.map_type, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.mapTypeNone:
            mgoogleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
            break;

        case R.id.mapTYpeNormale:
            mgoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            break;

        case R.id.mapTypeSatellite:
            mgoogleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
            break;

        case R.id.mapTYpeTerrain:
            mgoogleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            break;

        case R.id.mapTYpeHybrid:
            mgoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            break;
    }


    return super.onOptionsItemSelected(item);
}

LocationRequest mLocationRequest;

@Override
public void onConnected(@Nullable Bundle bundle) {

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(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;
        }
    }

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

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {

    if (location == null) {
        Toast.makeText(this, "can t 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);

    }


}

这些是权限

 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<permission android:name="pimproject.macbookproretina.example.com.pimproject.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="pimproject.macbookproretina.example.com.pimproject.MAPS_RECEIVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permissions.READ_GSERVICES"/>
<uses-feature android:glEsVersion="0x0020000" android:required="true"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

0 个答案:

没有答案