Add multiple tags to marker

不羁岁月 提交于 2021-02-20 04:47:17

问题


Created i model like in answer. Also added tags in markers:

 MarkerTag tag = new MarkerTag();
    Marker marker = map.addMarker(new MarkerOptions()
            .position(new LatLng(47.045029, 28.861427))
            .title("Marker")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
            .snippet("Population: 4,137,400"));
    tag.setEmail("first@gmail.com");
    tag.setPhoneNumber("+37369490007");
    marker.setTag(tag);
    Marker marker2 = map.addMarker(new MarkerOptions()
            .position(new LatLng(47.000327, 28.867950))
            .title("Marker")
            .snippet("Population: 4,137,400 fdfsfsfsdfsfdsfsfssfsfsfsfsfs"));
    tag.setEmail("second@gmail.com");
    tag.setPhoneNumber("+37368750588");
    marker2.setTag(tag);
    Marker marker3 = map.addMarker(new MarkerOptions()
            .position(new LatLng(47.024204, 28.889236))
            .title("Marker")
            .snippet("Population: 4,137,400 /n dasdasdas d"));
    tag.setEmail("three@gmail.com");
    tag.setPhoneNumber("+37369339092");
    marker3.setTag(tag);

And added my methods:

private void makeCall(Marker marker) {
    MarkerTag tag = ((MarkerTag)marker.getTag());
    if (tag != null) {
        Intent call = new Intent(Intent.ACTION_DIAL);
        call.setData(Uri.parse("tel:" + String.valueOf(tag.getPhoneNumber())));
        startActivity(call);
    }
}
private void sendEmail(Marker marker) {
    MarkerTag tag = ((MarkerTag)marker.getTag());
    if (tag != null) {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
        intent.putExtra(Intent.EXTRA_TEXT, "text");
        intent.setData(Uri.parse("email:" + String.valueOf(tag.getEmail())));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        checkAndStart(this, intent, R.string.action_settings);
    }
}

But now when i press on markers i get just one number, this

37369339092 and when i try to send email i catched the error : 233 26548-26548/com.vm.sai E/AndroidRuntime: FATAL EXCEPTION: main Process: com.vm.sai, PID: 26548 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object com.google.android.gms.maps.model.Marker.getTag()' on a null object reference at com.vm.sai.activities.MapsActivity.sendEmail(MapsActivity.java:168) at com.vm.sai.activities.MapsActivity.-wrap1(MapsActivity.java) at com.vm.sai.activities.MapsActivity$2.onClickConfirmed(MapsActivity.java:92) at com.vm.sai.utils.OnInfoWindowElemTouchListener$1.run(OnInfoWindowElemTouchListener.java:84) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5637) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)


回答1:


The .setTag() accept an object. So, in order to add more information in there you just have to create your own TagObject, lets call it MarkerTag;

public class MarkerTag {


    private String email;
    private String phoneNumber;

    public MarkerTag() {

    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}

Then you just add it to your marker as a tag:

MarkerTag yourMarkerTag = new MarkerTag();
yourMarkerTag.setEmail("someemail@gmail.com");
yourMarkerTag.setPhoneNumber("+15552345353");
marker.setTag(yourMarkerTag);

Now to get it back you just have to make sure that you are getting the right

    if(null != marker.getTag()){
    if(marker.getTag() instanceof MarkerTag){
        MarkerTag yourMarkerTag = ((MarkerTag)marker.getTag());
        Toast.makeText(getActivity, yourMarkerTag.getEmail() , Toast.LENGTH_SHORT).show();
        Toast.makeText(getActivity, yourMarkerTag.getPhoneNumber() , Toast.LENGTH_SHORT).show();
    }
}


来源:https://stackoverflow.com/questions/42892371/add-multiple-tags-to-marker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!