Java 应用程序在实现Google Api客户端和位置侦听器时崩溃
我正在尝试使用public void onLocationChanged(Location-Location)制作一个应用程序,在谷歌地图上跟踪用户的路径,但每次我尝试实现位置侦听器时,我的应用程序都会崩溃。这是我的密码。。如果您看到任何需要修复的问题,请告诉我。谢谢 Android清单文件Java 应用程序在实现Google Api客户端和位置侦听器时崩溃,java,android,location,Java,Android,Location,我正在尝试使用public void onLocationChanged(Location-Location)制作一个应用程序,在谷歌地图上跟踪用户的路径,但每次我尝试实现位置侦听器时,我的应用程序都会崩溃。这是我的密码。。如果您看到任何需要修复的问题,请告诉我。谢谢 Android清单文件 这是我的MapsActivity.java文件 package edu.carthage.haag.jacob.homotation\u application\u android; 导入androi
这是我的MapsActivity.java文件
package edu.carthage.haag.jacob.homotation\u application\u android;
导入android.Manifest;
导入android.content.Context;
导入android.content.pm.PackageManager;
导入android.graphics.Color;
导入android.location.location;
导入android.location.LocationManager;
导入android.os.Build;
导入android.support.annotation.NonNull;
导入android.support.annotation.Nullable;
导入android.support.v4.app.ActivityCompat;
导入android.support.v4.app.FragmentActivity;
导入android.os.Bundle;
导入android.support.v4.content.ContextCompat;
导入android.widget.Toast;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.google.android.gms.common.api.pendingreult;
导入com.google.android.gms.common.api.Status;
导入com.google.android.gms.location.LocationListener;
导入com.google.android.gms.location.LocationRequest;
导入com.google.android.gms.location.LocationServices;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.OnMapReadyCallback;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.BitmapDescriptorFactory;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.MarkerOptions;
导入com.google.android.gms.maps.model.Polyline;
导入com.google.android.gms.maps.model.PolylineOptions;
导入java.text.DateFormat;
导入java.util.ArrayList;
导入java.util.Date;
公共类MapsActivity扩展了FragmentActivity在MapreadyCallback、GoogleAppClient.ConnectionCallbacks、,
GoogleAppClient.OnConnectionFailedListener,LocationListener{
私有谷歌地图;
私有最终静态int MY_PERMISSION_FINE_LOCATION=101;
GoogleapClient MGoogleapClient;
私有ArrayList点;//已添加
多段线;//已添加
私人语境;
专用静态最终长间隔=5000;//1分钟
私有静态最终最长最快_间隔=1000;//1分钟
专用静态最终浮子最小位移=0.05F;//四分之一米
私人位置请求mLocationRequest;
私人场所经理;
专用浮动位置\刷新\距离=1;
专用长位置\刷新\时间=100;
//获取初始gps坐标
私人场所;
字符串位置提供者;
受保护的void createLocationRequest(){
//LocationServices.FusedLocationApi.RequestLocationUpdate(
//mgoogleapclient,mLocationRequest,this);
//
//if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)==PackageManager.permission\u已授予){
//LocationServices.FusedLocationApi.RequestLocationUpdate(mgoogleapClient、mlLocationRequest、this);
// }
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
mgoogleapclient=新的Googleapclient.Builder(此)
.addApi(LocationServices.API)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.build();
getMapAsync(这个);
points=新建ArrayList();//已添加
context=MapsActivity.this;
//createLocationRequest();
}
@凌驾
受保护的void onStart(){
super.onStart();
if(mGoogleApiClient!=null){
mGoogleApiClient.connect();
}
mlLocationRequest=新位置请求();
mlLocationRequest.setInterval(间隔);
mLocationRequest.SetFastTestInterval(最快间隔);
mlLocationRequest.setSmallestDisplacement(最小位移);//已添加
mLocationRequest.setPriority(位置请求.优先级高精度);
如果(ContextCompat.checkSelfPermission)(此,
清单.权限.访问(位置)
==PackageManager.权限(已授予){
LocationServices.FusedLocationApi.RequestLocationUpdate(mgoogleapClient、mlLocationRequest、this);
}
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
//在Sydney中添加标记并移动摄影机
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)==PackageManager.permission\u已授予){
mMap.setMyLocationEnabled(真);
}否则{
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
requestPermissions(新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},MY\u permission\u FINE\u LOCATION);
}
}
}
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
super.onRequestPermissionsResult(请求代码、权限、授权结果);
开关(请求代码){
案例我的\u许可\u罚款\u地点:
if(grantResults[0]==已授予PackageManager.权限){
if(ActivityCompat.checkSelfPermission)(此,Manif
if(mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
package edu.carthage.haag.jacob.homework_application_android;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;
import com.devop.aashish.androiddemo.R;
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.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;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import java.util.ArrayList;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, LocationListener, GoogleApiClient.OnConnectionFailedListener {
private final static int MY_PERMISSION_FINE_LOCATION = 101;
private static final long INTERVAL = 5000; //1 minute
private static final long FASTEST_INTERVAL = 1000; // 1 minute
private static final float SMALLEST_DISPLACEMENT = 0.05F; //quarter of a meter
GoogleApiClient mGoogleApiClient;
Polyline line; //added
String locationProvider;
private GoogleMap mMap;
private ArrayList<LatLng> points; //added
private Context context;
private LocationRequest mLocationRequest;
private LocationManager mLocationManager;
private float LOCATION_REFRESH_DISTANCE = 1;
private long LOCATION_REFRESH_TIME = 100;
//gets the initial gps coordinates
private Location mLastLocation;
protected void createLocationRequest() {
// LocationServices.FusedLocationApi.requestLocationUpdates(
// mGoogleApiClient, mLocationRequest, this);
//
// if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
// }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mapFragment.getMapAsync(this);
points = new ArrayList<LatLng>(); //added
context = MapsActivity.this;
//createLocationRequest();
}
@Override
protected void onStart() {
super.onStart();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT); //added
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
// LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
Toast.makeText(context, "Google API not connected yet", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_FINE_LOCATION);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSION_FINE_LOCATION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
}
} else {
Toast.makeText(getApplicationContext(), "This app requires location permissions to be granted", Toast.LENGTH_LONG).show();
finish();
}
break;
}
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude); //you already have this
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("New Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
points.add(latLng); //added
redrawLine(); //added
}
private void redrawLine() {
mMap.clear(); //clears all Markers and Polylines
PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int i = 0; i < points.size(); i++) {
LatLng point = points.get(i);
options.add(point);
}
//addMarker(); //add Marker in current position
line = mMap.addPolyline(options); //add Polyline
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (mGoogleApiClient.isConnected()) {
Toast.makeText(context, "Google API connected", Toast.LENGTH_SHORT).show();
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
Toast.makeText(context, "Google API not connected yet", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
// @Override
// public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// switch (requestCode) {
// case MY_PERMISSION_FINE_LOCATION:
// if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// mMap.setMyLocationEnabled(true);
// }
//
// } else {
// Toast.makeText(getApplicationContext(), "This app requires location permissions to be granted", Toast.LENGTH_LONG).show();
// finish();
// }
// break;
// }
// }
}
@Override
protected void onStart() {
super.onStart();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();