java - 如何在旅行时获取位置更新以跟踪其他设备的时间?
问题描述
我的项目是像优步一样从客户设备跟踪司机。我所做的是在 volley 库的帮助下获取驱动程序发送到服务器的位置数据,并将其发布到用户端的谷歌地图中。
我确实实现了处理程序来定期获取驱动程序发送的位置更新。但是我的应用程序崩溃了Unexpected response code 507和Unexpected response code 508。我只需要一些帮助,因为我怎样才能实现这一点,或者一些参考资料会有所帮助。
下面是我的代码。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
private final Handler handler = new Handler();
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
private GoogleMap mMap;
String newLatitude = "";
String newLongitude = "";
Marker marker;
Runnable runnable;
String schoolid, studentid;
String LocationUrl = Constant.BASE_URL+"selectlocation/";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
Bundle bundle = getIntent().getExtras();
schoolid = bundle.getString("ap_school_fkid");
studentid = bundle.getString("student_fkid");
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.maps);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}
}
@Override
public void onMapReady(final GoogleMap googleMap) {
Log.d("MapsActivity", "Inside onMapReady");
mMap = googleMap;
StringRequest request = new StringRequest(Request.Method.GET, LocationUrl+schoolid+"/"+studentid+"/", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("MapsActivity", response);
try {
JSONObject object = new JSONObject(response);
JSONArray array = object.getJSONArray("resp");
for (int i = 0; i < array.length(); i++) {
//JSONObject jsonObject = array.getJSONObject(i);
if (array.getJSONObject(i).getString("loc_latitude") != null && array.getJSONObject(i).getString("loc_longitude") != null) {
newLatitude = array.getJSONObject(i).getString("loc_latitude").trim();
newLongitude = array.getJSONObject(i).getString("loc_longitude").trim();
Toast.makeText(MapsActivity.this, newLatitude + ", " + newLongitude, Toast.LENGTH_SHORT).show();
addmarkerToMap(newLatitude, newLongitude);
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(request);
if (!newLatitude.isEmpty() && !newLongitude.isEmpty()) {
Double lat = Double.parseDouble(newLatitude);
Double lng = Double.parseDouble(newLongitude);
LatLng latLng = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(latLng).title("I'm Here"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
CameraUpdate location = CameraUpdateFactory.newLatLngZoom(latLng, 17);
mMap.animateCamera(location);
}
}
private void addmarkerToMap(String newLat, String newLng) {
double lat = Double.parseDouble(newLat);
double lng = Double.parseDouble(newLng);
LatLng latLng = new LatLng(lat, lng);
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(lat, lng)).title("I'm Here");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
CameraUpdate location = CameraUpdateFactory.newLatLngZoom(latLng, 17);
mMap.animateCamera(location);
new Handler().postDelayed(() -> {
getLocation();
}, 3000);
}
private void getLocation(){
Log.d("getLocation", "Refreshed");
mMap.clear();
if (mMap!= null){
if (marker == null){
StringRequest request = new StringRequest(Request.Method.GET, LocationUrl+schoolid+"/"+studentid+"/", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject object = new JSONObject(response);
JSONArray array = object.getJSONArray("resp");
for (int i = 0; i < array.length(); i++) {
//JSONObject jsonObject = array.getJSONObject(i);
if (array.getJSONObject(i).getString("loc_latitude") != null && array.getJSONObject(i).getString("loc_longitude") != null) {
newLatitude = array.getJSONObject(i).getString("loc_latitude").trim();
newLongitude = array.getJSONObject(i).getString("loc_longitude").trim();
Toast.makeText(MapsActivity.this, "location is refreshed, new Latitude : "+newLatitude+", new Longitude : "+newLongitude, Toast.LENGTH_SHORT).show();
addmarkerToMap(newLatitude, newLongitude);
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(request);
if (!newLatitude.isEmpty() && !newLongitude.isEmpty()) {
Double lat = Double.parseDouble(newLatitude);
Double lng = Double.parseDouble(newLongitude);
LatLng latLng = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(latLng).title("I'm Here"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
CameraUpdate location = CameraUpdateFactory.newLatLngZoom(latLng, 17);
mMap.animateCamera(location);
}
}
}
}
}
解决方案
推荐阅读
- javascript - 如何将 POST(javascript) 发送到 laravel 刀片文件?
- oracle - 使用 Oracle 应用程序警报插入表
- javascript - iOS Swift 上的 javascript 需要等到通过 XMLHttpRequest 加载动态内容
- python - Python - 烧瓶 BadRequestKeyError:400 错误请求:KeyError
- vba - 如何将代码模块中包含的VBA代码放入工作表模块中
- docker - Docker 日志填满正在运行的容器
- ruby-on-rails - 使用 twurl api 时 Rails 控制台上的 twitter 错误
- android - startActivityForResult 在片段中崩溃
- java - Java 中具有 LRU 缓存的线程安全集合
- ios - 如何在swift中的uitextview中创建可点击的文本