Mapbox SDK:在运行时更改标记位置

时间:2019-02-01 17:32:54

标签: java android mapbox

我试图定位在mapbox,使用每30秒一个服务器数据更新它的位置的点。我该怎么做呢?我正在使用7.0.1版。到目前为止,我设法显示了一个标记,但是在运行时无法更改其位置。所述标记物接受来自服务器的数据,但在启动时仅达

我尝试下面的代码,但是当我尝试运行它在我的模拟器崩溃了:

package com.example.susmi.rubus;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Main2Activity extends AppCompatActivity {

private MapView mapView;
String lat = "xx", lon = "xy";
MapboxMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Mapbox.getInstance(this, getString(R.string.access_token));
    setContentView(R.layout.activity_main2);
    Bundle bundle = getIntent().getExtras();
    String s = bundle.getString("xyz").toUpperCase();
    String html = "https://rubus.arduinocommunity.org.bd/api/view_data.php?fbclid=IwAR0pyaB4ZDQhrwAJ99vqAdRmSgAgvpBpKwg4RkgIeJDLu6wiNvWqGCJqFbQ";
    //The thread is only for parsing data from the server
    Thread t = new Thread(() -> {
        try {
            int count = 0;
            Document doc = Jsoup.connect(html).get();
            Element body = doc.body();
            Elements paragraphs = body.getElementsByTag("td");
            for (Element para : paragraphs) {
                String s2 = para.text().toUpperCase();
                if (s2.equals(s)) {
                    count++;
                } else if (count != 0)
                    count++;
                if (count == 3) {
                    lat = s2;
                } else if (count == 4) {
                    lon = s2;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    t.start();
    try {
        t.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    mapView = findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(@NonNull final MapboxMap mapboxMap) {
            map = mapboxMap;
            mapboxMap.setStyle(Style.LIGHT, new Style.OnStyleLoaded() {
                @Override
                public void onStyleLoaded(@NonNull Style style) {
// Add the marker image to map
                    double s1 = Double.parseDouble(lat), t = Double.parseDouble(lon);
                    style.addImage("marker-icon-id",
                            BitmapFactory.decodeResource(
                                    Main2Activity.this.getResources(), R.drawable.mapbox_marker_icon_default));
                    GeoJsonSource geoJsonSource = new GeoJsonSource("source-id", Feature.fromGeometry(
                            Point.fromLngLat(t, s1)));
                    style.addSource(geoJsonSource);

                    SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id");
                    symbolLayer.withProperties(
                            PropertyFactory.iconImage("marker-icon-id")
                    );
                    style.addLayer(symbolLayer);
                    updateMarker(s, html);
                }
            });
        }
    });
}

public void updateMarker(String s, String html) {
    while(true) {
            //The thread is only for parsing data from the server
        Thread t1 = new Thread(() -> {
            try{
                Thread.sleep(3000);
            }catch(InterruptedException e)
            {
                e.printStackTrace();
            }
            try {
                int count = 0;
                Document doc = Jsoup.connect(html).get();
                Element body = doc.body();
                Elements paragraphs = body.getElementsByTag("td");
                for (Element para : paragraphs) {
                    String s2 = para.text().toUpperCase();
                    if (s2.equals(s)) {
                        count++;
                    } else if (count != 0)
                        count++;
                    if (count == 3) {
                        lat = s2;
                    } else if (count == 4) {
                        lon = s2;
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        });
            t1.start();
            try{
               t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        if (map.getStyle() != null) {
            GeoJsonSource g = map.getStyle().getSourceAs("source-id");
            if (g != null) {
                g.setGeoJson(FeatureCollection.fromFeature(
                        Feature.fromGeometry(Point.fromLngLat(88, 24))//Double.parseDouble(lon), Double.parseDouble(lat)))
                ));
            }
        }
    }
}
// Add the mapView's own lifecycle methods to the activity's lifecycle methods
@Override
public void onStart() {
    super.onStart();
    mapView.onStart();
}

@Override
public void onResume() {
    super.onResume();
    mapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mapView.onPause();
}

@Override
public void onStop() {
    super.onStop();
    mapView.onStop();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
}
}    

1 个答案:

答案 0 :(得分:0)

问题在于,由于updateMarker()中的while循环,onStyleLoaded方法始终无法完成,因此我的模拟器处于空白状态。谢谢那些回答的人。