我的标记出现问题,因为每次Latitude
和Longitude
更改时,它都会创建一个新标记而不是覆盖旧标记。我正在获取多个标记,因此我将其存储在ArrayList
内。
我的String busRoute
和int passenger
也变为空。所以这是我的代码。我希望有人能解决我的问题。
private String name, busNum, busRoute, currentLocation, estimatedTime, availableSeat, lat, lng;
double latitude, longitude;
int passenger;
public void markerDriver() {
markerArray = new ArrayList<>();
Firebase ref = new Firebase(Config.FIREBASE_URL_DRIVER);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getChildrenCount() == 0) {
markerInfo();
} else {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if (snapshot.child("availableSeat").exists() && snapshot.child("latitude").exists() && snapshot.child("longitude").exists() && snapshot.child("estimatedTime").exists()) {
name = snapshot.child("driversName").getValue().toString().trim();
busNum = snapshot.child("busNum").getValue().toString().trim();
lat = snapshot.child("latitude").getValue().toString().trim();
lng = snapshot.child("longitude").getValue().toString().trim();
availableSeat = snapshot.child("availableSeat").getValue().toString().trim();
estimatedTime = snapshot.child("estimatedTime").getValue().toString().trim();
latitude = Double.valueOf(lat);
longitude = Double.valueOf(lng);
convertLatLong();
getTotalPass();
markerArray.add(new Driver(name, busNum, busRoute, passenger, latitude, longitude, currentLocation, estimatedTime));
}
}
for (i = 0; i < markerArray.size(); i++) {
createMarker(markerArray.get(i).getDriversName(), markerArray.get(i).getBusNum(), markerArray.get(i).getRoute(), markerArray.get(i).getTotalPassenger(), markerArray.get(i).getLatitude(), markerArray.get(i).getLongitude(), markerArray.get(i).getLocation(), markerArray.get(i).getEstimatedTime());
}
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(MainMapActivity.this, "markerDriver: " + firebaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
public void convertLatLong() {
Geocoder geocoder;
List<android.location.Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.get(0).getAddressLine(0) != null) {
currentLocation = addresses.get(0).getAddressLine(0) + ", " + addresses.get(0).getLocality();
} else {
currentLocation = addresses.get(0).getLocality();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void getTotalPass() {
busRef = new Firebase(Config.FIREBASE_URL_BUSNUMBER);
Query bus = busRef.orderByChild("busNum").equalTo(busNum);
bus.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
passenger = Integer.valueOf(snapshot.child("capacity").getValue().toString().trim()) - Integer.valueOf(availableSeat);
busRoute = snapshot.child("route").getValue().toString().trim();
Toast.makeText(MainMapActivity.this, "routeGetTotalPass: " + busRoute + ":::" + passenger, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(MainMapActivity.this, "getTotalPass: " + firebaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
createMarker
protected void createMarker(String driversName, final String busNum, String route, final int totalPass, double latitude, double longitude, String location, String estimatedTime) {
Marker marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(location)
.snippet("Driver's Name: " + driversName + "\nBus number: " + busNum + "\nRoute: " + route + "\nTotal Passenger: " + totalPass + "\nEstimated time: " + estimatedTime));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
dialogInfo(marker);
return true;
}
});
}
答案 0 :(得分:0)
在添加新标记
之前删除旧标记 if (marker != null) {
marker.remove();
}
convertLatLong();
getTotalPass();
markerArray.add(new Driver(name, busNum, busRoute, passenger, latitude, longitude, currentLocation, estimatedTime));
另一种方法是在添加新标记
之前清除GoogleMapgoogleMap.clear();
在onCreate声明之前
标记标记;
然后改变
protected void createMarker(String driversName, final String busNum, String route, final int totalPass, double latitude, double longitude, String location, String estimatedTime) {
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(location)
.snippet("Driver's Name: " + driversName + "\nBus number: " + busNum + "\nRoute: " + route + "\nTotal Passenger: " + totalPass + "\nEstimated time: " + estimatedTime));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
dialogInfo(marker);
return true;
}
});
}
答案 1 :(得分:0)
首先声明标记
ArrayList<Marker> markerList=new ArrayList<>();
的列表
然后将您的方法创建标记更改为
protected void createMarker(String driversName, final String busNum, String route, final int totalPass, double latitude, double longitude, String location, String estimatedTime) {
boolean isOld = false;
for (Marker m:markerList) {
if (m.getTitle().equals(location)) {
m.setPosition(new LatLng(latitude, longitude));
isOld=true;
break;
}
}
if (!isOld) {
Marker marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(location)
.snippet("Driver's Name: " + driversName + "\nBus number: " + busNum + "\nRoute: " + route + "\nTotal Passenger: " + totalPass + "\nEstimated time: " + estimatedTime));
markerList.add(marker);
}
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
dialogInfo(marker);
return true;
}
});
}
这对我有用
答案 2 :(得分:0)
如果你想删除特定的标记,那么使用带有整数作为id和marker对象的weakhash map,然后你可以根据索引id删除它。
有关详情,请查看此内容 - https://stackoverflow.com/a/35363423/2128166
答案 3 :(得分:0)
我使用以下代码制作:
string first_value = "4";
string second_value = "5";
//compare two values
if ( first_value < second_value){
cout << "4 is smaller than 5" <<endl;
}else{
cout << "5 is smaller than 4" <<endl;
}