gpt4 book ai didi

java - Mapbox SDK : Changing a marker position at runtime

转载 作者:太空狗 更新时间:2023-10-29 14:36:30 24 4
gpt4 key购买 nike

我正在尝试在 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);
}
}

最佳答案

问题在于,由于 updateMarker() 中的 while 循环,onStyleLoaded 方法从未完成,因此我的模拟器保持空白。感谢那些回答的人。

关于java - Mapbox SDK : Changing a marker position at runtime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54484424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com