Reputation: 45
I made JSON includes latitude and Longitude on My car. It's refreshing every 1 second. I retrieve this data using Retrofit and it's working. Earlier I display this data on textview's.
For now I have two issues. How can I place this two variables into google Map to create marker (or blue dot) and how can I save last known position (to retrieve it anytime when I haven't got connection with server. In application I don't wanna use build-in GPS receiver, so location manager may be useless
main activity:
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;
}
}
my model class:
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;
}
}
Upvotes: 0
Views: 423
Reputation: 972
In GoogleMap a blue dot represents the location of the device, and you get it like this:
GoogleMap googleMap;
googleMap.setMyLocationEnabled(true);
To create a Marker with Latitude and Longitude of you car you can do this:
LatLng myCarLocation = new LatLng(latitude, longitude);
Marker myCarMarker = googleMap.addMarker(new MarkerOptions().position(myCarLocation).title("My Car Marker"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(myCarLocation));
And to save last know location i would suggest using Shared Preferences to save location every time you get the last location, and then use it whenever you need it.
Upvotes: 1