向标记添加多个标记

时间:2017-03-19 21:16:12

标签: java android google-maps google-maps-markers

在答案中创建了i模型。 还在标记中添加了标记:

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

并添加了我的方法:

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

但现在当我按下标记时,我只得到一个数字,这个

  

37369339092   当我尝试发送电子邮件时,我抓住了错误:   233 26548-26548 / com.vm.sai E / AndroidRuntime:FATAL EXCEPTION:main                                                               处理:com.vm.sai,PID:26548                                                               java.lang.NullPointerException:尝试调用虚方法   'java.lang.Object com.google.android.gms.maps.model.Marker.getTag()'   在null对象引用上                                                                   在com.vm.sai.activities.MapsActivity.sendEmail(MapsActivity.java:168)                                                                   在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)                                                                   在android.os.Handler.handleCallback(Handler.java:815)                                                                   在android.os.Handler.dispatchMessage(Handler.java:104)                                                                   在android.os.Looper.loop(Looper.java:194)                                                                   在android.app.ActivityThread.main(ActivityThread.java:5637)                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                   在java.lang.reflect.Method.invoke(Method.java:372)                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:959)                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

1 个答案:

答案 0 :(得分:3)

.setTag()接受object。因此,为了在那里添加更多信息,你只需要创建自己的TagObject,我们称之为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;
    }
}

然后,您只需将其作为标记添加到标记中:

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

现在要取回它,你必须确保你得到正确的

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