Funkcióinformációk megjelenítése
Feljegyzés
Az Azure Térképek Android SDK kivonása
Az Androidhoz készült Azure Térképek Natív SDK elavult, és 25.31-én megszűnik. A szolgáltatáskimaradások elkerülése érdekében 25.31-ig migráljon az Azure Térképek Web SDK-ba. További információ: Az Azure Térképek Android SDK migrálási útmutatója.
A térbeli adatokat gyakran pontokkal, vonalakkal és sokszögekkel ábrázolják. Ezek az adatok gyakran metaadat-információkkal vannak társítva. Egy pont például egy étterem helyét jelölheti, és az étterem metaadatai lehetnek az általa kiszolgált ételek neve, címe és típusa. Ez a metaadatok a GeoJSON Feature
tulajdonságaiként vehetők fel. Az alábbi kód létrehoz egy egyszerű pont funkciót egy title
olyan tulajdonsággal, amelynek értéke ""Helló világ!" alkalmazás!"
//Create a data source and add it to the map.
DataSource source = new DataSource();
map.sources.add(source);
//Create a point feature.
Feature feature = Feature.fromGeometry(Point.fromLngLat(-122.33, 47.64));
//Add a property to the feature.
feature.addStringProperty("title", "Hello World!");
//Create a point feature, pass in the metadata properties, and add it to the data source.
source.add(feature);
//Create a data source and add it to the map.
val source = DataSource()
map.sources.add(source)
//Create a point feature.
val feature = Feature.fromGeometry(Point.fromLngLat(-122.33, 47.64))
//Add a property to the feature.
feature.addStringProperty("title", "Hello World!")
//Create a point feature, pass in the metadata properties, and add it to the data source.
source.add(feature)
Az adatok térképhez való létrehozásával és hozzáadásával kapcsolatos további információkért lásd : Adatforrás létrehozása.
Amikor egy felhasználó egy funkcióval kommunikál a térképen, az események segítségével reagálhat ezekre a műveletekre. Gyakori forgatókönyv, hogy a felhasználó által használt szolgáltatás metaadat-tulajdonságaiból álló üzenet jelenik meg. Az OnFeatureClick
esemény az a fő esemény, amely észleli, ha a felhasználó leképezett egy funkciót a térképen. Van egy OnLongFeatureClick
esemény is. Amikor hozzáadja az OnFeatureClick
eseményt a térképhez, az egyetlen rétegre korlátozható, ha egy réteg azonosítóját adja meg, és arra korlátozza azt. Ha nem ad át rétegazonosítót, a térkép egyik funkciójára koppintva , függetlenül attól, hogy melyik rétegben van, aktiválja ezt az eseményt. Az alábbi kód létrehoz egy szimbólumréteget a térkép pontadatainak megjelenítéséhez, majd hozzáad egy eseményt OnFeatureClick
, és korlátozza a szimbólumréteget.
//Create a symbol and add it to the map.
SymbolLayer layer = new SymbolLayer(source);
map.layers.add(layer);
//Add a feature click event to the map.
map.events.add((OnFeatureClick) (features) -> {
//Retrieve the title property of the feature as a string.
String msg = features.get(0).getStringProperty("title");
//Do something with the message.
//Return a boolean indicating if event should be consumed or continue bubble up.
return false;
}, layer.getId()); //Limit this event to the symbol layer.
//Create a symbol and add it to the map.
val layer = SymbolLayer(source)
map.layers.add(layer)
//Add a feature click event to the map.
map.events.add(OnFeatureClick { features: List<Feature> ->
//Retrieve the title property of the feature as a string.
val msg = features[0].getStringProperty("title")
//Do something with the message.
//Return a boolean indicating if event should be consumed or continue bubble up.
return false
}, layer.getId()) //Limit this event to the symbol layer.
Bejelentési üzenet megjelenítése
A bejelentési üzenet az egyik legegyszerűbb módja annak, hogy információkat jelenítsen meg a felhasználó számára, és az Android minden verziójában elérhető. Semmilyen típusú felhasználói bemenetet nem támogat, és csak rövid ideig jelenik meg. Ha szeretne gyorsan tudatni a felhasználóval valamit arról, hogy mit leképeztek, a bejelentési üzenet jó választás lehet. Az alábbi kód bemutatja, hogyan használható egy bejelentési üzenet az OnFeatureClick
eseményhez.
//Add a feature click event to the map.
map.events.add((OnFeatureClick) (features) -> {
//Retrieve the title property of the feature as a string.
String msg = features.get(0).getStringProperty("title");
//Display a toast message with the title information.
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
//Return a boolean indicating if event should be consumed or continue bubble up.
return false;
}, layer.getId()); //Limit this event to the symbol layer.
//Add a feature click event to the map.
map.events.add(OnFeatureClick { features: List<Feature> ->
//Retrieve the title property of the feature as a string.
val msg = features[0].getStringProperty("title")
//Display a toast message with the title information.
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
//Return a boolean indicating if event should be consumed or continue bubble up.
return false
}, layer.getId()) //Limit this event to the symbol layer.
A bejelentési üzenetek mellett számos más módon is megjelenítheti egy szolgáltatás metaadat-tulajdonságait, például:
- A snackbar widget -
Snackbars
egyszerű visszajelzést ad egy műveletről. Egy rövid üzenet jelenik meg a képernyő alján mobilon, a nagyobb eszközökön pedig bal alsó sarokban.Snackbars
minden más elem felett jelenik meg a képernyőn, és egyszerre csak egy jeleníthető meg. - Párbeszédpanelek – A párbeszédpanel egy kis ablak, amely arra kéri a felhasználót, hogy döntsön, vagy adjon meg további információkat. A párbeszédpanelek nem töltik ki a képernyőt, és általában olyan modális eseményekhez használatosak, amelyek megkövetelik, hogy a felhasználók műveletet hajtanak végre a folytatás előtt.
- Töredék hozzáadása az aktuális tevékenységhez.
- Navigáljon egy másik tevékenységhez vagy nézethez.
Előugró ablak megjelenítése
Az Azure Térképek Android SDK egy osztályt Popup
biztosít, amely megkönnyíti a felhasználói felületi széljegyzetelemek létrehozását, amelyek a térkép egy pozíciójához vannak rögzítve. Előugró ablakok esetén relatív elrendezésű nézetet kell átadnia az content
előugró ablak lehetőségének. Íme egy egyszerű elrendezési példa, amely sötét szöveget jelenít meg a háttérben.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#ffffff"
android:layout_margin="8dp"
android:padding="10dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:text=""
android:textSize="18dp"
android:textColor="#222"
android:layout_height="wrap_content"
android:width="200dp"/>
</RelativeLayout>
Feltéve, hogy a fenti elrendezés egy alkalmazás mappájában lévő popup_text.xml
fájlban res -> layout
van tárolva, az alábbi kód létrehoz egy előugró kódot, és hozzáadja a térképhez. Ha egy funkcióra kattint, a tulajdonság az title
popup_text.xml
elrendezés használatával jelenik meg, az elrendezés alsó közepe pedig a térképen megadott pozícióhoz van rögzítve.
//Create a popup and add it to the map.
Popup popup = new Popup();
map.popups.add(popup);
map.events.add((OnFeatureClick)(feature) -> {
//Get the first feature and it's properties.
Feature f = feature.get(0);
JsonObject props = f.properties();
//Retrieve the custom layout for the popup.
View customView = LayoutInflater.from(this).inflate(R.layout.popup_text, null);
//Access the text view within the custom view and set the text to the title property of the feature.
TextView tv = customView.findViewById(R.id.message);
tv.setText(props.get("title").getAsString());
//Get the position of the clicked feature.
Position pos = MapMath.getPosition((Point)cluster.geometry());
//Set the options on the popup.
popup.setOptions(
//Set the popups position.
position(pos),
//Set the anchor point of the popup content.
anchor(AnchorType.BOTTOM),
//Set the content of the popup.
content(customView)
//Optionally, hide the close button of the popup.
//, closeButton(false)
//Optionally offset the popup by a specified number of pixels.
//pixelOffset(new Pixel(10, 10))
);
//Open the popup.
popup.open();
//Return a boolean indicating if event should be consumed or continue bubble up.
return false;
});
//Create a popup and add it to the map.
val popup = Popup()
map.popups.add(popup)
map.events.add(OnFeatureClick { feature: List<Feature> ->
//Get the first feature and it's properties.
val f = feature[0]
val props = f.properties()
//Retrieve the custom layout for the popup.
val customView: View = LayoutInflater.from(this).inflate(R.layout.popup_text, null)
//Access the text view within the custom view and set the text to the title property of the feature.
val tv: TextView = customView.findViewById(R.id.message)
tv.text = props!!["title"].asString
//Get the position of the clicked feature.
val pos = MapMath.getPosition(f.geometry() as Point?);
//Set the options on the popup.
popup.setOptions(
//Set the popups position.
position(pos),
//Set the anchor point of the popup content.
anchor(AnchorType.BOTTOM),
//Set the content of the popup.
content(customView)
//Optionally, hide the close button of the popup.
//, closeButton(false)
//Optionally offset the popup by a specified number of pixels.
//pixelOffset(Pixel(10, 10))
)
//Open the popup.
popup.open()
//Return a boolean indicating if event should be consumed or continue bubble up.
false
})
Az alábbi képernyőfelvételen az előugró ablakok jelennek meg, amikor a funkciókra kattintanak, és a térképen a megadott helyre vannak rögzítve, miközben mozognak.
Következő lépések
További adatok hozzáadása a térképhez: