使用 JSON 在 Google 地图 API 中设置位置并保存最后已知的位置

Set Location in Google Maps API using JSON and save last known Location

我JSON在我的车上制作了纬度和经度。每1秒刷新一次。我使用 Retrofit 检索了这些数据并且它正在工作。之前我在 textview 上显示了这些数据。

现在我有两个问题。如何将这两个变量放入 google 地图中以创建标记(或蓝点)以及如何保存最后一个已知位置(以便在我没有与服务器建立连接时随时检索它。 在应用程序中我不想使用内置 GPS 接收器,所以位置管理器可能没用

主要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;

}

}

我的模型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;
}
}

在 GoogleMap 中,一个蓝点代表设备的位置,您可以这样获取:

GoogleMap googleMap;
googleMap.setMyLocationEnabled(true);

要创建带有您汽车的经纬度的标记,您可以这样做:

   LatLng myCarLocation = new LatLng(latitude, longitude);
   Marker myCarMarker = googleMap.addMarker(new MarkerOptions().position(myCarLocation).title("My Car Marker"));
   googleMap.moveCamera(CameraUpdateFactory.newLatLng(myCarLocation));

为了保存最后知道的位置,我建议您在每次获得最后一个位置时使用共享首选项来保存位置,然后在需要时使用它。