【发布时间】:2016-02-03 15:30:08
【问题描述】:
我有一个名为 LocationTest 的活动,在我的应用程序中获取纬度和经度并且它工作正常。此活动包括一个按钮,可将我们带到另一个名为 MapLocation 的活动,它以图形方式显示地图上的位置。但是当我单击该应用程序将停止工作。如果有人可以提供帮助,我将发布 2 个活动的代码。我认为错误出现在第二个活动中
位置测试活动
public class LocationTest extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
private FusedLocationProviderApi locationProvider = LocationServices.FusedLocationApi;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
public final static int MILLISECONDS_PER_SECOND=1000;
public final static int MINUTE = 60*MILLISECONDS_PER_SECOND;
private double longitude=0;
private double latitude=0;
private TextView lblLong;
private TextView lblLat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_test);
//this here is for the map
Button mapBtn =(Button)findViewById(R.id.mapBtn);
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mapBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(LocationTest.this,MapLocation.class);
startActivity(intent);
}
});
locationRequest= new LocationRequest();
/*locationRequest.setInterval(MINUTE);*/
locationRequest.setInterval((MINUTE));
locationRequest.setFastestInterval(15 * MILLISECONDS_PER_SECOND);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
lblLong = (TextView)findViewById(R.id.lblLong);
lblLat = (TextView)findViewById(R.id.lblLat);
}
@Override
protected void onStart() {
super.onStart();
googleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
googleApiClient.disconnect();
}
@Override
protected void onResume() {
super.onResume();
if(googleApiClient.isConnected()){
requestLocationUpdates();
}
}
@Override
public void onConnected(Bundle bundle) {
requestLocationUpdates();
}
private void requestLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(this, "Your Location Has Been Set: " + location.getLatitude() + " " + location.getLongitude(), Toast.LENGTH_LONG).show();
longitude = location.getLongitude();
latitude = location.getLatitude();
lblLong.setText(Double.toString(longitude));
lblLat.setText(Double.toString(latitude));
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
地图定位活动
public class MapLocation extends AppCompatActivity implements LocationListener{
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isGooglePlayServicesAvailable()) {
finish();
}
setContentView(R.layout.activity_map_location);
SupportMapFragment supportMapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
googleMap = supportMapFragment.getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}
@Override
public void onLocationChanged(Location location) {
TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
return false;
}
}
}
【问题讨论】:
-
我是移动开发新手,我不知道什么是崩溃日志。你能帮我写代码吗?
标签: android google-maps geolocation maps