Skip to content

Ví dụ về thêm marker có onclick và MarkerView

activity_marker_marker_view.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="asia.wemap.androidsdktestapp.MarkerInfoActivity">


    <asia.wemap.androidsdk.WeMapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        android:layout_gravity="end"

    </asia.wemap.androidsdk.WeMapView>
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:src="@drawable/ic_baseline_my_location_24"
        android:layout_margin="16dp" />
</FrameLayout>

marker_view_bubble.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="200dp"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/cardview_dark_background"
    app:cardCornerRadius="10dp"
    app:cardElevation="5dp"
    app:cardUseCompatPadding="true"
    android:layout_height="wrap_content"
    tools:ignore="MissingDefaultResource">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/marker_window_title"
            android:layout_width="wrap_content"
            android:padding="8dp"
            tools:text="Text here is this"
            android:textStyle="bold"
            android:textColor="@color/black_semi_transparent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/marker_window_snippet"
            android:layout_width="wrap_content"
            android:padding="8dp"
            tools:text="Text here is this"
            android:textColor="@color/black_semi_transparent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</androidx.cardview.widget.CardView>

MarkerInfoActivity.java

package asia.wemap.androidsdktestapp;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import asia.wemap.androidsdk.OnMarkerClickListener;
import asia.wemap.androidsdk.WeMap;
import asia.wemap.androidsdk.WeMapMap;
import asia.wemap.androidsdk.WeMapView;
import asia.wemap.androidsdk.WeMapOptions;
import asia.wemap.androidsdk.annotaion.WeMapMarker;
import asia.wemap.androidsdk.geometry.LatLng;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

public class MarkerInfoActivity extends AppCompatActivity {
    private WeMapView mapView;
    private static final String ICON_ID = "ICON_ID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WeMap.getInstance(this, getString(R.string.access_token));
        LatLng center =  new LatLng(21, 105);
        WeMapOptions options = new WeMapOptions(this, center, 4);

        mapView = new WeMapView(this, options);
        mapView.onCreate(savedInstanceState);
        setContentView(mapView);
        mapView.getWeMapMapAsync(new WeMapMap.OnWeMapReadyCallback() {
            @Override
            public void onMapReady(@NonNull WeMapMap wemapMap) {
                wemapMap.addOnMapClickListener(new WeMapMap.OnMapClickListener() {
                    @Override
                    public boolean onMapClick(LatLng point) {
                        WeMapMarker marker = wemapMap.createMarker(new LatLng(point), ICON_ID);
                        marker.set("name", String.valueOf(point.getLatitude()) + ", " + String.valueOf(point.getLongitude()));
                        return true;
                    }
                } );

                wemapMap.onMarkerClick(new OnMarkerClickListener() {
                    @Override
                    public void OnMarkerClick(WeMapMarker marker) {
                        Log.d("test", marker.getMarkerGeometry().toString());

                        //Add markerView
                        View customView = LayoutInflater.from(MarkerInfoActivity.this).inflate(
                                R.layout.marker_view_bubble, null);
                        customView.setLayoutParams(new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));

                        TextView titleTextView = customView.findViewById(R.id.marker_window_title);
                        titleTextView.setText(marker.get("name"));

                        TextView snippetTextView = customView.findViewById(R.id.marker_window_snippet);
                        snippetTextView.setText("Hello, World!");
                        wemapMap.createViewMarker(marker.getMarkerGeometry(), customView);
                    }

                    @Override
                    public void OnMarkerLongClick(WeMapMarker marker) {
                        //Xoa marker
                        wemapMap.removeMarker(marker);
                        //Xoa het marker
//                        wemapMap.removeAllMarker();
                        Log.d("test", marker.getMarkerGeometry().toString());
                    }
                });

                //Add marker with image
                //Add Image
                wemapMap.addImage(ICON_ID, BitmapFactory.decodeResource(getResources(), R.drawable.marker2));
                //Add Marker
                WeMapMarker marker = wemapMap.createMarker(new LatLng(21, 105), ICON_ID);
                marker.set("name", "WeMap");

            }
        });
    }

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

    @Override
    @SuppressWarnings({"MissingPermission"})
    protected void onStart() {
        super.onStart();
        mapView.onStart();
    }

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

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

    @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);
    }

}