从一个方法调用另一个
Invoke one method from another
我想通过public void message()
方法调用private void addMarkers()
方法。如果有人可以帮助我,我会 grateful.I 需要这样做,因为 message()
方法接收纬度,所以我希望每次它收到一条消息(带有纬度和经度)它调用 addMarker()
方法,以便可以使用消息方法中接收到的新经纬度重新创建它。如果有人能帮我找到另一种添加 Maker 及其更新的方法,我将接受,从现在开始,感激不尽。
我的代码:
public void pubnubActions() {
pubnub.addListener(new SubscribeCallback() {
@Override
public void status(PubNub pubnub, PNStatus status) {
switch (status.getOperation()) {
// let's combine unsubscribe and subscribe handling for ease of use
case PNSubscribeOperation:
case PNUnsubscribeOperation:
// note: subscribe statuses never have traditional
// errors, they just have categories to represent the
// different issues or successes that occur as part of subscribe
switch (status.getCategory()) {
case PNConnectedCategory:
// this is expected for a subscribe, this means there is no error or issue whatsoever
case PNReconnectedCategory:
// this usually occurs if subscribe temporarily fails but reconnects. This means
// there was an error but there is no longer any issue
case PNDisconnectedCategory:
// this is the expected category for an unsubscribe. This means there
// was no error in unsubscribing from everything
case PNUnexpectedDisconnectCategory:
// this is usually an issue with the internet connection, this is an error, handle appropriately
case PNAccessDeniedCategory:
// this means that PAM does allow this client to subscribe to this
// channel and channel group configuration. This is another explicit error
default:
// More errors can be directly specified by creating explicit cases for other
// error categories of `PNStatusCategory` such as `PNTimeoutCategory` or `PNMalformedFilterExpressionCategory` or `PNDecryptionErrorCategory`
}
case PNHeartbeatOperation:
// heartbeat operations can in fact have errors, so it is important to check first for an error.
// For more information on how to configure heartbeat notifications through the status
// PNObjectEventListener callback, consult <link to the PNCONFIGURATION heartbeart config>
if (status.isError()) {
// There was an error with the heartbeat operation, handle here
} else {
// heartbeat operation was successful
}
default: {
// Encountered unknown status type
}
}
}
@Override
public void message(PubNub pubnub, PNMessageResult message) {
String messagePublisher = message.getPublisher();
System.out.println("Message publisher: " + messagePublisher);
System.out.println("Message Payload: " + message.getMessage());
System.out.println("Message Subscription: " + message.getSubscription());
System.out.println("Message Channel: " + message.getChannel());
System.out.println("Message timetoken: " + message.getTimetoken());
JsonObject mensagem = (JsonObject)message.getMessage();
String latitude = mensagem.get("lat").getAsString();
String longitude = mensagem.get("lng").getAsString();
String altitude = mensagem.get("alt").getAsString();
Lat = Double.parseDouble(latitude);
Lng = Double.parseDouble(longitude);
alt = Double.parseDouble(altitude);
System.out.println("A latitude recebida é igual: "+ latitude);
System.out.println("A longitude recebida é igual: "+ longitude);
System.out.println("A altura recebida é igual: "+ altitude);
}
@Override
public void presence(PubNub pubnub, PNPresenceEventResult presence) {
}
});
pubnub.subscribe()
.channels(Arrays.asList("")) // subscribe to channels
.execute();
}
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
Mapa.this.mapboxMap = mapboxMap;
mapboxMap.setStyle(new Style.Builder().fromUri(""),
new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent(style);
style.addImage(MARKER_IMAGE, BitmapFactory.decodeResource(
Mapa.this.getResources(), R.drawable.automobile));
addMarkers(style);
}
private void addMarkers(@NonNull Style loadedMapStyle) {
List<Feature> features = new ArrayList<>();
features.add(Feature.fromGeometry(Point.fromLngLat(Lat, Lng)));
/* Source: A data source specifies the geographic coordinate where the image marker gets placed. */
loadedMapStyle.addSource(new GeoJsonSource(MARKER_SOURCE, FeatureCollection.fromFeatures(features)));
/* Style layer: A style layer ties together the source and image and specifies how they are displayed on the map. */
loadedMapStyle.addLayer(new SymbolLayer(MARKER_STYLE_LAYER, MARKER_SOURCE)
.withProperties(
PropertyFactory.iconAllowOverlap(true),
PropertyFactory.iconIgnorePlacement(true),
PropertyFactory.iconImage(MARKER_IMAGE),
// Adjust the second number of the Float array based on the height of your marker image.
// This is because the bottom of the marker should be anchored to the coordinate point, rather
// than the middle of the marker being the anchor point on the map.
PropertyFactory.iconOffset(new Float[]{-10f, -0f})
));
}
});
}
如果您想重构代码以便从消息中获取位置数据,您可以这样做。不知道您要做什么,就很难提供解决方案。
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
Mapa.this.mapboxMap = mapboxMap;
mapboxMap.setStyle(new Style.Builder().fromUri(""),
new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent(style);
style.addImage(MARKER_IMAGE, BitmapFactory.decodeResource(
Mapa.this.getResources(), R.drawable.automobile));
mStyle = style; //mStyle is class wide member variable
}
}
private void addMarkers(@NonNull Style loadedMapStyle, Lat lat, Lng lng) {
List<Feature> features = new ArrayList<>();
features.add(Feature.fromGeometry(Point.fromLngLat(lat, lng)));
/* Source: A data source specifies the geographic coordinate where the image marker gets placed. */
loadedMapStyle.addSource(new GeoJsonSource(MARKER_SOURCE, FeatureCollection.fromFeatures(features)));
/* Style layer: A style layer ties together the source and image and specifies how they are displayed on the map. */
loadedMapStyle.addLayer(new SymbolLayer(MARKER_STYLE_LAYER, MARKER_SOURCE)
.withProperties(
PropertyFactory.iconAllowOverlap(true),
PropertyFactory.iconIgnorePlacement(true),
PropertyFactory.iconImage(MARKER_IMAGE),
// Adjust the second number of the Float array based on the height of your marker image.
// This is because the bottom of the marker should be anchored to the coordinate point, rather
// than the middle of the marker being the anchor point on the map.
PropertyFactory.iconOffset(new Float[]{-10f, -0f})
));
}
});
}
然后在您的消息方法中,您可以通过如下方式传递样式和位置数据来添加标记。
Lat = Double.parseDouble(latitude);
Lng = Double.parseDouble(longitude);
alt = Double.parseDouble(altitude);
addMarkers(mStyle, Lat, Lng);
logcat当我运行代码
05-08 22:37:33.129 8310-8401/com.example.wheresmybus E/AndroidRuntime: FATAL EXCEPTION: Subscription Manager Consumer Thread
Process: com.example.wheresmybus, PID: 8310
com.mapbox.mapboxsdk.exceptions.CalledFromWorkerThreadException: Mbgl-Source interactions should happen on the UI thread.
at com.mapbox.mapboxsdk.utils.ThreadUtils.checkThread(ThreadUtils.java:43)
at com.mapbox.mapboxsdk.style.sources.Source.checkThread(Source.java:45)
at com.mapbox.mapboxsdk.style.sources.Source.<init>(Source.java:38)
at com.mapbox.mapboxsdk.style.sources.GeoJsonSource.<init>(GeoJsonSource.java:187)
at com.example.wheresmybus.Mapa.addMarkers(Mapa.java:226)
at com.example.wheresmybus.Mapa.access[=10=]0(Mapa.java:65)
at com.example.wheresmybus.Mapa.message(Mapa.java:181)
at com.pubnub.api.managers.ListenerManager.announce(ListenerManager.java:45)
at com.pubnub.api.workers.SubscribeMessageWorker.processIncomingPayload(SubscribeMessageWorker.java:197)
at com.pubnub.api.workers.SubscribeMessageWorker.takeMessage(SubscribeMessageWorker.java:60)
at com.pubnub.api.workers.SubscribeMessageWorker.run(SubscribeMessageWorker.java:51)
at java.lang.Thread.run(Thread.java:818)
at com.example.wheresmybus.Mapa.addMarkers(Mapa.java:226)
= loadedMapStyle.addSource(new GeoJsonSource(MARKER_SOURCE, FeatureCollection.fromFeatures(features)));
出现的所有这些其他错误不会干扰应用程序的运行,因为在我添加标记之前出现了这些相同的错误并且应用程序正常运行
我想通过public void message()
方法调用private void addMarkers()
方法。如果有人可以帮助我,我会 grateful.I 需要这样做,因为 message()
方法接收纬度,所以我希望每次它收到一条消息(带有纬度和经度)它调用 addMarker()
方法,以便可以使用消息方法中接收到的新经纬度重新创建它。如果有人能帮我找到另一种添加 Maker 及其更新的方法,我将接受,从现在开始,感激不尽。
我的代码:
public void pubnubActions() {
pubnub.addListener(new SubscribeCallback() {
@Override
public void status(PubNub pubnub, PNStatus status) {
switch (status.getOperation()) {
// let's combine unsubscribe and subscribe handling for ease of use
case PNSubscribeOperation:
case PNUnsubscribeOperation:
// note: subscribe statuses never have traditional
// errors, they just have categories to represent the
// different issues or successes that occur as part of subscribe
switch (status.getCategory()) {
case PNConnectedCategory:
// this is expected for a subscribe, this means there is no error or issue whatsoever
case PNReconnectedCategory:
// this usually occurs if subscribe temporarily fails but reconnects. This means
// there was an error but there is no longer any issue
case PNDisconnectedCategory:
// this is the expected category for an unsubscribe. This means there
// was no error in unsubscribing from everything
case PNUnexpectedDisconnectCategory:
// this is usually an issue with the internet connection, this is an error, handle appropriately
case PNAccessDeniedCategory:
// this means that PAM does allow this client to subscribe to this
// channel and channel group configuration. This is another explicit error
default:
// More errors can be directly specified by creating explicit cases for other
// error categories of `PNStatusCategory` such as `PNTimeoutCategory` or `PNMalformedFilterExpressionCategory` or `PNDecryptionErrorCategory`
}
case PNHeartbeatOperation:
// heartbeat operations can in fact have errors, so it is important to check first for an error.
// For more information on how to configure heartbeat notifications through the status
// PNObjectEventListener callback, consult <link to the PNCONFIGURATION heartbeart config>
if (status.isError()) {
// There was an error with the heartbeat operation, handle here
} else {
// heartbeat operation was successful
}
default: {
// Encountered unknown status type
}
}
}
@Override
public void message(PubNub pubnub, PNMessageResult message) {
String messagePublisher = message.getPublisher();
System.out.println("Message publisher: " + messagePublisher);
System.out.println("Message Payload: " + message.getMessage());
System.out.println("Message Subscription: " + message.getSubscription());
System.out.println("Message Channel: " + message.getChannel());
System.out.println("Message timetoken: " + message.getTimetoken());
JsonObject mensagem = (JsonObject)message.getMessage();
String latitude = mensagem.get("lat").getAsString();
String longitude = mensagem.get("lng").getAsString();
String altitude = mensagem.get("alt").getAsString();
Lat = Double.parseDouble(latitude);
Lng = Double.parseDouble(longitude);
alt = Double.parseDouble(altitude);
System.out.println("A latitude recebida é igual: "+ latitude);
System.out.println("A longitude recebida é igual: "+ longitude);
System.out.println("A altura recebida é igual: "+ altitude);
}
@Override
public void presence(PubNub pubnub, PNPresenceEventResult presence) {
}
});
pubnub.subscribe()
.channels(Arrays.asList("")) // subscribe to channels
.execute();
}
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
Mapa.this.mapboxMap = mapboxMap;
mapboxMap.setStyle(new Style.Builder().fromUri(""),
new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent(style);
style.addImage(MARKER_IMAGE, BitmapFactory.decodeResource(
Mapa.this.getResources(), R.drawable.automobile));
addMarkers(style);
}
private void addMarkers(@NonNull Style loadedMapStyle) {
List<Feature> features = new ArrayList<>();
features.add(Feature.fromGeometry(Point.fromLngLat(Lat, Lng)));
/* Source: A data source specifies the geographic coordinate where the image marker gets placed. */
loadedMapStyle.addSource(new GeoJsonSource(MARKER_SOURCE, FeatureCollection.fromFeatures(features)));
/* Style layer: A style layer ties together the source and image and specifies how they are displayed on the map. */
loadedMapStyle.addLayer(new SymbolLayer(MARKER_STYLE_LAYER, MARKER_SOURCE)
.withProperties(
PropertyFactory.iconAllowOverlap(true),
PropertyFactory.iconIgnorePlacement(true),
PropertyFactory.iconImage(MARKER_IMAGE),
// Adjust the second number of the Float array based on the height of your marker image.
// This is because the bottom of the marker should be anchored to the coordinate point, rather
// than the middle of the marker being the anchor point on the map.
PropertyFactory.iconOffset(new Float[]{-10f, -0f})
));
}
});
}
如果您想重构代码以便从消息中获取位置数据,您可以这样做。不知道您要做什么,就很难提供解决方案。
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
Mapa.this.mapboxMap = mapboxMap;
mapboxMap.setStyle(new Style.Builder().fromUri(""),
new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent(style);
style.addImage(MARKER_IMAGE, BitmapFactory.decodeResource(
Mapa.this.getResources(), R.drawable.automobile));
mStyle = style; //mStyle is class wide member variable
}
}
private void addMarkers(@NonNull Style loadedMapStyle, Lat lat, Lng lng) {
List<Feature> features = new ArrayList<>();
features.add(Feature.fromGeometry(Point.fromLngLat(lat, lng)));
/* Source: A data source specifies the geographic coordinate where the image marker gets placed. */
loadedMapStyle.addSource(new GeoJsonSource(MARKER_SOURCE, FeatureCollection.fromFeatures(features)));
/* Style layer: A style layer ties together the source and image and specifies how they are displayed on the map. */
loadedMapStyle.addLayer(new SymbolLayer(MARKER_STYLE_LAYER, MARKER_SOURCE)
.withProperties(
PropertyFactory.iconAllowOverlap(true),
PropertyFactory.iconIgnorePlacement(true),
PropertyFactory.iconImage(MARKER_IMAGE),
// Adjust the second number of the Float array based on the height of your marker image.
// This is because the bottom of the marker should be anchored to the coordinate point, rather
// than the middle of the marker being the anchor point on the map.
PropertyFactory.iconOffset(new Float[]{-10f, -0f})
));
}
});
}
然后在您的消息方法中,您可以通过如下方式传递样式和位置数据来添加标记。
Lat = Double.parseDouble(latitude);
Lng = Double.parseDouble(longitude);
alt = Double.parseDouble(altitude);
addMarkers(mStyle, Lat, Lng);
logcat当我运行代码
05-08 22:37:33.129 8310-8401/com.example.wheresmybus E/AndroidRuntime: FATAL EXCEPTION: Subscription Manager Consumer Thread
Process: com.example.wheresmybus, PID: 8310
com.mapbox.mapboxsdk.exceptions.CalledFromWorkerThreadException: Mbgl-Source interactions should happen on the UI thread.
at com.mapbox.mapboxsdk.utils.ThreadUtils.checkThread(ThreadUtils.java:43)
at com.mapbox.mapboxsdk.style.sources.Source.checkThread(Source.java:45)
at com.mapbox.mapboxsdk.style.sources.Source.<init>(Source.java:38)
at com.mapbox.mapboxsdk.style.sources.GeoJsonSource.<init>(GeoJsonSource.java:187)
at com.example.wheresmybus.Mapa.addMarkers(Mapa.java:226)
at com.example.wheresmybus.Mapa.access[=10=]0(Mapa.java:65)
at com.example.wheresmybus.Mapa.message(Mapa.java:181)
at com.pubnub.api.managers.ListenerManager.announce(ListenerManager.java:45)
at com.pubnub.api.workers.SubscribeMessageWorker.processIncomingPayload(SubscribeMessageWorker.java:197)
at com.pubnub.api.workers.SubscribeMessageWorker.takeMessage(SubscribeMessageWorker.java:60)
at com.pubnub.api.workers.SubscribeMessageWorker.run(SubscribeMessageWorker.java:51)
at java.lang.Thread.run(Thread.java:818)
at com.example.wheresmybus.Mapa.addMarkers(Mapa.java:226)
= loadedMapStyle.addSource(new GeoJsonSource(MARKER_SOURCE, FeatureCollection.fromFeatures(features)));
出现的所有这些其他错误不会干扰应用程序的运行,因为在我添加标记之前出现了这些相同的错误并且应用程序正常运行