package project.vtrac.tracker;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import android.app.Dialog;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
public class MapView extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
private Double latitude;
private Double longitude;
private Double speed;
private int curTime;
private int lastTime;
private Double lastLat;
private Double lastLng;
TextView msgArea;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapview);
checkGooglePlayService();
}
public void checkGooglePlayService()
{
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if(status!=ConnectionResult.SUCCESS) // Google Play Services are not available
{
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}
else
{
createMap();
}
}
public void createMap()
{
// Getting reference to the SupportMapFragment of map.xml
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = mapFrag.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
googleMap.getUiSettings().setCompassEnabled(true);
//Start location manager to start location updates
startLocationUpdate();
}
public void startLocationUpdate()
{
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
lastLat = location.getLatitude();
lastLng = location.getLongitude();
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 5000, 10, this);
}
@Override
public void onLocationChanged(Location location) {
// Getting latitude of the current location
latitude = location.getLatitude();
// Getting longitude of the current location
longitude = location.getLongitude();
//get the current date time in reference to milliseconds from location.getTime()
Date curDate = new Date(location.getTime());
Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
calendar.setTime(curDate); //assign calender to given date
//convert time to seconds
curTime = (int) ((calendar.get(Calendar.HOUR_OF_DAY)*60 + calendar.get(Calendar.MINUTE))*60 + calendar.get(Calendar.SECOND));
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(16));
msgArea = (TextView) findViewById(R.id.tv_msgArea);
String msg;
msg = "\tLatLong:" + latitude + "," + longitude+
"\tLatLong:" + lastLat + "," + lastLng+
"\t Speed:"+speed;
msgArea.setText(msg);
//calculateSpeed
calculateSpeedThread();
lastTime = curTime;
lastLat = latitude;
lastLng = 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
}
public void calculateSpeedThread()
{
Thread sThread = new Thread(null, doCalculateSpeed, "Speed Calculator");
sThread.start();
}
private Runnable doCalculateSpeed = new Runnable()
{
public void run()
{
speed = calculateSpeed();
}
};
public double calculateSpeed()
{
double R = 6371.00; // km
double lat1 = lastLat * (Math.PI/180);
double lat2 = latitude * (Math.PI/180);
double lng1 = lastLng * (Math.PI/180);
double lng2 = longitude * (Math.PI/180);
double dLat = (lat2-lat1);
double dLon = (lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double distance = R*c;
double timeDiff = (double)(curTime-lastTime);
//speed in km/hr
return ((distance/timeDiff) * 60 * 60);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
我的速度返回null。我想要一些帮助! 没有使用线程速度很好但是当我使用线程来减少函数开销调用时间时,我需要使用线程来计算速度。
我的线程编程有问题吗?
答案 0 :(得分:0)
我不太确定你想要什么,但我在你的代码中发现了一个错误。在创建线程时,您将 null 作为第一个参数传递。尽量不要使用它。我想你可以用, MapView.this 而不是