我正在创建一个应用以在地图上显示用户的位置。问题是,当调用方法requestLocationUpdates或removeUpdates时,我在LocationListener参数中收到错误。我知道这可能是由于导入了错误的文件,但我确实导入了com.google.android.gms.location.LocationListener。 我做了我的研究,但真的找不到解决方案。我会把一些代码放在一边,这样可能有所帮助。 谢谢你的帮助。
import com.google.android.gms.location.LocationListener;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,LocationListener {
private GoogleMap mMap;
LocationManager locationManager;
String provider;
@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);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(),false);
}
@Override
public void onResume(){
super.onResume();
locationManager.requestLocationUpdates(provider,400,1,this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng cali = new LatLng(3.451234, -76.532694);
mMap.moveCamera(CameraUpdateFactory.newLatLng(cali));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
@Override
public void onLocationChanged(Location location) {
Double lat = location.getAltitude();
Double lng = location.getLongitude();
}
@Override
protected void onPause(){
super.onPause();
locationManager.removeUpdates(this);
}
}
正如我所说的那样,问题在于:
locationManager.requestLocationUpdates(provider,400,1,this);
和
locationManager.removeUpdates(this);
顺便说一句,如果我删除这两行并且只是显示地图和标记,那么代码没有问题。
答案 0 :(得分:0)
更改,按行
import com.google.android.gms.location.LocationListener;
用
import android.location.LocationListener ;