我正在尝试绘制一条路线,因为用户使用GPS移动但出现了问题,并且没有显示该行。我不确定问题是什么。 任何人都可以向我指出我做了什么错误。 提前致谢
public class MainActivity extends FragmentActivity implements OnMyLocationChangeListener, OnMapClickListener, OnMapLongClickListener {
GoogleMap g1;
private double longitude = -1;
private double latitude = -1;
private double lastLocationTime = 0;
Polyline line, distance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment sf1 = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
g1 = sf1.getMap();
g1.setMapType(GoogleMap.MAP_TYPE_NORMAL);
g1.setMyLocationEnabled(true);
g1.setOnMapClickListener(this);
g1.setOnMapLongClickListener( this);
g1.setOnMyLocationChangeListener(this);
distance = line;
}
@Override
public void onMapClick(LatLng arg0) { }
@Override
public void onMapLongClick(LatLng arg0) {}
@Override
public void onMyLocationChange(Location location) {
TextView Location = (TextView) findViewById(R.id.textView1);
double oldlat = latitude;
double oldlong = longitude;
latitude = calcLatitude(location, latitude);
longitude = calcLongitude(location, longitude);
Location.setText("Latitude:" + latitude + ", Longitude:"+ longitude );
line = g1.addPolyline(new PolylineOptions().add(new LatLng(oldlat, oldlong), new LatLng(latitude, longitude)).width(5).color(Color.RED));}
private double calcLongitude(Location location, double oldlongitude) {
double newlongitude = oldlongitude;
if(oldlongitude == -1)
{oldlongitude = newlongitude;}
if(location.getAccuracy() <= 30)
{
newlongitude = (2 * oldlongitude + location.getLatitude()) / (2 + 1);
lastLocationTime = System.currentTimeMillis();
}
if (lastLocationTime >= 2) {
newlongitude = location.getLatitude();
}
return newlongitude;
}
private double calcLatitude(Location location, double oldlatitude) { double newlatitude = oldlatitude;
if(oldlatitude == -1){
oldlatitude = newlatitude; }
if(location.getAccuracy() <= 30){
newlatitude = (2 * oldlatitude + location.getLatitude()) / (2 + 1);
lastLocationTime = System.currentTimeMillis();
}
if (lastLocationTime >= 2) {
newlatitude = location.getLatitude();
}
return newlatitude;
}
}