我开发了一个应用程序,用于绘制用户移动的折线。 现在我有一个GoogleMap,当我启动这个应用程序时,我得到了一条折线,从未知位置到我的位置。我只是想在用户开始移动时开始绘制这条折线。 任何人都可以告诉我我的代码有什么问题吗?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("MYTAG","onCreate()");
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
if (mGoogleApiClient != null)
Log.d("MYTAG","Google api created!");
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000) // 1 second, in milliseconds
.setSmallestDisplacement(10);
}
@Override
public void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
public void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mFusedLocationCallback);
mGoogleApiClient.disconnect();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.run_fragment, container, false);
} catch (InflateException e) {
// Map already created
}
if (mMap == null) {
// Get the map fragment
mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.myMapid)).getMap();
// If get map successfully set "Go to my location" button enabled
if (mMap != null)
mMap.setMyLocationEnabled(true);
}
// Timer widgets
tv_timer = (TextView)view.findViewById(R.id.timer);
b_start_activity = (Button)view.findViewById(R.id.b_start_activity);
b_start_activity.setOnClickListener(this);
// CountDown widgets
tv_CountDown = (TextView)view.findViewById(R.id.tv_countdown);
iv_CountDownBack = (ImageView)view.findViewById(R.id.iv_countdown);
// CountDown object
countDownTimer = new CounterClass(5000,1000);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (mMap != null) {
getChildFragmentManager().beginTransaction()
.remove(getChildFragmentManager().findFragmentById(R.id.myMapid)).commitAllowingStateLoss();
mMap = null;
}
}
@Override
public void onConnected(Bundle bundle) {
location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
Log.d("MYTAG","Requesting location updates!");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mFusedLocationCallback);
} else
updateUI(location);
Log.d("MYTAG", "GoogleApi is connected! And location time is: " + location.getTime());
}
@Override
public void onConnectionSuspended(int i) {
Log.d("MYTAG", "GoogleApi connection suspended!");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(getActivity(), CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
} else {
Log.i("MYTAG", "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
private void updatePolyline(LatLng tempLatlng, LatLng currentLatlng) {
Log.d("MYTAG","Polyline updated!");
Polyline line = mMap.addPolyline(new PolylineOptions()
.add(tempLatlng, currentLatlng)
.width(10)
.color(Color.BLUE));
line.setVisible(true);
}
private void updateUI(Location location) {
Log.d("MYTAG", "Updating your location!");
LatLng temp = new LatLng(latitude, longitude);
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
// move camera to current location and zoom in
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
updatePolyline(temp,latLng);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.b_start_activity){
iv_CountDownBack.setVisibility(View.VISIBLE);
tv_CountDown.setVisibility(View.VISIBLE);
b_start_activity.setVisibility(View.GONE);
countDownTimer.start();
}
}
private class LocationCallback implements LocationListener{
public LocationCallback(){
}
@Override
public void onLocationChanged(Location location) {
Log.d("MYTAG", "Location changed");
LatLng temp = new LatLng(latitude, longitude); // Store previouse points
// Get new points
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
// Call to updateUI to update the UI with correct location on map
updateUI(location);
}
}