【问题标题】:Set Location in Google Maps API using JSON and save last known Location使用 JSON 在 Google Maps API 中设置位置并保存最后一个已知位置
【发布时间】: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


    【解决方案1】:

    在 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));
    

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

    【讨论】:

    • 感谢您的回复。共享首选项是一个很好的提示,但是您对标记的建议我在上面的代码中有但它不起作用。
    • 我已经更新了我的答案。我认为它(标记)只需要一个标题。我在下面添加了一条额外的线,以便谷歌地图移动到该位置,您可以验证。希望有帮助!
    • 好的,问题出在 onCreate 方法中。工作正常。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    相关资源
    最近更新 更多