我将JSON包含在汽车上的纬度和经度。每1秒钟刷新一次。我使用Retrofit检索了此数据,并且可以正常工作。早些时候,我在textview上显示了这些数据。
目前,我有两个问题。如何将这两个变量放入Google Map中以创建标记(或蓝点),以及如何保存上一个已知位置(在与服务器没有连接时随时检索它)。 在应用程序中,我不想使用内置的GPS接收器,因此位置管理器可能没用
主要活动:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
LocationManager locationManager;
public static final String BASE_URL = "ip_of_server";
private static Retrofit retrofit = null;
private Handler mHandler = new Handler();
public Double lat;
public Double lon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
getDataRunnable.run();
}
public void getData() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
Call<OutputModel> call = jsonPlaceHolderApi.getCords();
call.enqueue(new Callback<OutputModel>() {
@Override
public void onResponse(Call<OutputModel> call, retrofit2.Response<OutputModel> response) {
if (!response.isSuccessful()) {
// onFailTV.setText("Code: " + response.code());
return;
}
lat = response.body().getLatitude();
lon = response.body().getLongitude();
LatLng carPosition = new LatLng(lat, lon);
mMap.addMarker(new MarkerOptions().position(carPosition).title("Integra Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(carPosition, 15.5f));
}
@Override
public void onFailure(Call<OutputModel> call, Throwable t) {
// onFailTV.setText(t.getMessage());
}
});
}
public Runnable getDataRunnable = new Runnable() {
@Override
public void run() {
getData();
mHandler.postDelayed(this,1000);
}
};
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
}
我的模型班:
public class OutputModel {
@SerializedName("latitude")
private Double latitude;
@SerializedName("longitude")
private Double longitude;
@SerializedName("velocity")
private Double velocity;
public OutputModel(Double latitude, Double longitude, Double velocity) {
this.latitude = latitude;
this.longitude = longitude;
this.velocity = velocity;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getVelocity() {
return velocity;
}
public void setVelocity(Double velocity) {
this.velocity = velocity;
}
}
答案 0 :(得分:1)
在GoogleMap中,一个蓝点代表设备的位置,您可以这样获得它:
Type
要创建具有汽车经纬度的标记,您可以执行以下操作:
RuntimeType
要保存最后知道的位置,我建议每次使用“共享首选项”保存位置时都保存该位置,然后在需要时使用它。