在Asynctask中从Google Maps添加标记 [英] Adding markers from Google Maps in Asynctask

查看:85
本文介绍了在Asynctask中从Google Maps添加标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AsyncTask中完成某些操作后,我想向Google Maps添加标记.但是,当我尝试在操作后在onPostExecute上添加标记时,将会发生错误:

I want to add a marker to Google Maps after I've done something in AsyncTask. However, when I attempt to add a marker onPostExecute after the operation, An error will occur:

 java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference

我搜索了此问题,但没有帮助.如何在Asyncask的OnPostExecute内添加标记?

I searched for this problem, but it didn't help. How do I add a marker inside OnPostExecute in Asyncask?

这是我的代码的安排.直到DoInBackground都可以正常工作.

This is an arrangement of my code. It works normally until DoInBackground.

 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
GoogleMap mMap;
Document doc = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap googleMap) {
    GetXMLTask task = new GetXMLTask(getApplicationContext());
    task.execute("http://openapi.its.go.kr:8081/api/NCCTVInfo?key=1526041502162&ReqType=2&MinX=" + 126 + "&MaxX=" + 127 + "&MinY=" + 35 + "&MaxY=" + 38 + "&type=ex");
}

@SuppressLint("NewApi")
private class GetXMLTask extends AsyncTask<String, Void, Document> {

    public GetXMLTask(Context applicationContext) {
    }

    @Override
    protected Document doInBackground(String... urls) {
        URL url;

        try {
        } catch (Exception e) {
        }
        return doc;
    }

    @Override
    protected void onPostExecute(Document doc) {
        MarkerOptions makerOptions = new MarkerOptions();
        makerOptions
                .position(new LatLng(0, 0))
                .title("title");
       mMap.addMarker(makerOptions);  //<=====ERROR=====
        }
    }
}

推荐答案

mMap对象未设置任何内容.因此它将返回null.您可以这样设置:

mMap object didn't set anything. So it will return null. You can set like that:

@Override
public void onMapReady(final GoogleMap googleMap) {
    mMap = googleMap;
    GetXMLTask task = new GetXMLTask(getApplicationContext());
    task.execute("http://openapi.its.go.kr:8081/api/NCCTVInfo?key=1526041502162&ReqType=2&MinX=" + 126 + "&MaxX=" + 127 + "&MinY=" + 35 + "&MaxY=" + 38 + "&type=ex");
}

这篇关于在Asynctask中从Google Maps添加标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆