Google地图标记未显示在用户位置

时间:2016-09-28 10:15:22

标签: android google-maps google-maps-android-api-2

我的应用程序上有一个googlemap但是标记没有显示用户的位置和标记应该在那里,每次用户更新其位置时标记也应该更新。你能帮帮我们吗?谢谢:))

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

GoogleMap mGoogleMap;
GoogleApiClient mGoogleAPIClient;
MarkerOptions options;
final String TAG = this.getClass().getName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(googleServicesAvailable()){
        setContentView(R.layout.activity_gpsrenter);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initMap();
    }else{

    }

}

private void initMap() {
    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment);
    mapFragment.getMapAsync(this);
}

public boolean googleServicesAvailable(){
    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int isAvailable = api.isGooglePlayServicesAvailable(this);
    if(isAvailable == ConnectionResult.SUCCESS){
        return true;
    }else if(api.isUserResolvableError(isAvailable)){
        Dialog dialog = api.getErrorDialog(this, isAvailable, 0);
        dialog.show();
    }else{
        Toast.makeText(this, "Cant connect to Google Play Services", Toast.LENGTH_SHORT).show();
    }
    return false;
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;
//        goToLocationZoom(39.008224, -76.8984527, 15);
//        mGoogleMap.setMyLocationEnabled(true);

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


LocationRequest mLocationRequest;

@Override
public void onConnected(Bundle bundle) {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(5000);

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

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
    if(location == null){
        Toast.makeText(this, "Can't get current location", Toast.LENGTH_SHORT).show();
    }else{

        LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
        Log.d(TAG, String.valueOf(location.getLatitude()));
        Log.d(TAG, String.valueOf(location.getLongitude()));
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, 18);

        MarkerOptions options = new MarkerOptions()
                .position(new LatLng(location.getLatitude(),location.getLatitude()))
                .snippet("I am here");

        mGoogleMap.addMarker(options);

        mGoogleMap.animateCamera(update);

    }
}
}

1 个答案:

答案 0 :(得分:0)

@ Batz Pogi试着希望这可以帮到你..

Marker marker = googleMap.addMarker(new MarkerOptions()
               .position(new LatLng(location.getLatitude(),location.getLatitude()))
           .icon(BitmapDescriptorFactory.fromResource(R.drawable.yourImage)));

如需更多信息,请点击以下链接..

https://developers.google.com/maps/documentation/android-api/marker