【发布时间】:2019-11-27 20:55:47
【问题描述】:
我在我的车上制作了包含纬度和经度的 JSON。每1秒刷新一次。我使用 Retrofit 检索这些数据并且它正在工作。之前我在 textview 上显示这些数据。
现在我有两个问题。如何将这两个变量放入谷歌地图以创建标记(或蓝点)以及如何保存最后一个已知位置(在我没有与服务器连接时随时检索它。 在应用程序中我不想使用内置 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;
}
}
【问题讨论】:
标签: android google-maps geolocation