简体   繁体   English

x秒后更新标记

[英]Update markers after x seconds

I'm developing an android application and I have a google map on one of the activities. 我正在开发一个Android应用程序,我在其中一个活动上有一个谷歌地图。 The map contains a marker and the coordinates of the marker will be updated every 10 seconds. 地图包含标记,标记的坐标将每10秒更新一次。 The coordinates will be obtained from a server. 坐标将从服务器获得。 Question: How do I update the marker's position on the map? 问题:如何更新标记在地图上的位置?

Here is what my mapsActivity.java looks like: 这是我的mapsActivity.java的样子:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

private String lat = "-25.4316562";
private String lon = "50.6602371";

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

}

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    LatLng sydney = new LatLng(-100, 35);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker on the drone"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(17);
    mMap.animateCamera(zoom);
}

I have 2 functions that will update the global variables lat and lon by sending SSH commands to the server: 我有2个函数将通过向服务器发送SSH命令来更新全局变量lat和lon:

new getXCoordinates().execute();
new getYCoordinates().execute();

I tried calling a function to update the coordinates and marker in the OnCreate function, but that's not working. 我试着调用一个函数来更新OnCreate函数中的坐标和标记,但这不起作用。 I cant find out how to call a function repeatedly after 10 seconds to update the marker. 我无法找到如何在10秒后重复调用函数来更新标记。

You can use something like this: 你可以使用这样的东西:

Handler h = new Handler();
int delay = 10 * 1000;

h.postDelayed(new Runnable(){
    public void run(){

        // This portion of code runs each 10s.

        h.postDelayed(this, delay);
    }
}, delay);

to add a marker you should use 添加您应该使用的标记

yourMarker = googleMap.addMarker(new MarkerOptions().position(latlng))

and to update use 并更新使用

yourMarker.setPosition(latlng);

The key here is to retain an instance of the created marker so you can change its position at will. 这里的关键是保留创建的标记的实例,以便您可以随意更改其位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM