【问题标题】:Android markers not showing up asynctask mapsAndroid 标记未显示异步任务映射
【发布时间】:2017-12-19 00:43:59
【问题描述】:

我正在尝试让标记显示在地图上。它从 MySQL 数据库中获取标记 lat 和 long 并在 asynctask 中使用 JSON 检索它,我在日志中得到以下内容。我认为这与谷歌证书有关,但我不确定。此代码适用于旧版本的 android

I/zzbx: Making Creator dynamically
W/zygote: Skipping duplicate class check due to unrecognized classloader
I/Google Maps Android API: Google Play services client version: 11020000
I/Google Maps Android API: Google Play services package version: 11743470
I/zygote: Do partial code cache collection, code=125KB, data=68KB
I/zygote: After code cache collection, code=120KB, data=67KB
I/zygote: Increasing code cache capacity to 512KB
I/Choreographer: Skipped 66 frames!  The application may be doing too much work on its main thread.
W/Google Maps Android API: Deprecation notice: In a future release, indoor will no longer be supported on satellite, hybrid or terrain type maps. Even where indoor is not supported, isIndoorEnabled() will continue to return the value that has been set via setIndoorEnabled(), as it does now. By default, setIndoorEnabled is 'true'. The API release notes (https://developers.google.com/maps/documentation/android-api/releases) will let you know when indoor support becomes unavailable on those map types.
D/EGL_emulation: eglMakeCurrent: 0x9a51aee0: ver 2 0 (tinfo 0x9a7ff8a0)

             [ 12-18 19:32:33.017  6391: 6463 D/         ]
             HostConnection::get() New Host Connection established 0x8abe1340, tid 6463
D/EGL_emulation: eglCreateContext: 0x82ad6f60: maj 1 min 0 rcv 1
D/EGL_emulation: eglMakeCurrent: 0x82ad6f60: ver 1 0 (tinfo 0x823a57e0)
W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
W/zygote: Skipping duplicate class check due to unrecognized classloader
I/DynamiteModule: Considering local module   com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 4
W/zygote: Skipping duplicate class check due to unrecognized classloader

这是我的代码

public class MapsActivity extends FragmentActivity implements      GoogleMap.OnMarkerClickListener, OnMapReadyCallback {

private Marker marker;
private GoogleMap mMap;
public String lastClick;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);


}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    mMap.setOnMarkerClickListener(this);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    mMap.setMyLocationEnabled(true);

    new AsyncTaskGetMareker().execute();
}

public String getJSONFromAssets() {
    String json = null;
    try {
        Intent intent = getIntent();
        final String cID = intent.getStringExtra("cID");
        InputStream inputData = new URL("someurlhere" + cID).openStream();
        int size = inputData.available();
        byte[] buffer = new byte[size];
        inputData.read(buffer);
        inputData.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}




private class AsyncTaskGetMareker extends AsyncTask<String, String, JSONArray> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected JSONArray doInBackground(String... strings) {
        String stationsJsonString = getJSONFromAssets();
        try {
            JSONArray stationsJsonArray = new JSONArray(stationsJsonString);
            return stationsJsonArray;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //This will only happen if an exception is thrown above:
        return null;
    }

    protected void onPostExecute(JSONArray result) {
        if (result != null) {
            for (int i = 0; i < result.length(); i++) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = result.getJSONObject(i);
                    String name= jsonObject.getString("name");
                    String lat = jsonObject.getString("lat");
                    String lng = jsonObject.getString("lng");
                    String occupied = jsonObject.getString("occupied");
                    String id = jsonObject.getString("id");
                    String hunb = jsonObject.getString("hunb");
                    int occ = Integer.parseInt(occupied);
                    drawMarker(new LatLng(Double.parseDouble(lat),
                            Double.parseDouble(lng)), name, occ, id, hunb);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private void drawMarker(LatLng point, String name, int occ, String id, String hunb) {

        mMap.addMarker(new MarkerOptions()
                .position(point)
                .title(name)
                .snippet(id)
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

    }

}
}

【问题讨论】:

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


    【解决方案1】:

    您应该在 onCreate() 方法的开头调用 api,以便当您的片段创建该时间时,它会向服务器发送请求以获取记录,并且另一端的谷歌地图将准备就绪。请尝试应用此逻辑。

    【讨论】:

    • 你是说在 oncreate 和 onmapready 中调用异步任务?
    【解决方案2】:

    查看您的代码,您似乎没有请求位置权限。 它可能会进入 return 语句,并且您在 map ready 上的 asynctask 语句没有被执行。

    只需检查在 app gradle 中设置 target_sdk 22。

    这仅用于测试目的。在访问地图对象上的 mylocationenabled(true) 之前,您需要执行请求权限的代码。

    【讨论】:

      【解决方案3】:

      您需要检查是否授予位置权限。 由于在执行 AysncTask 之前您的代码中有 return 语句,它可能没有执行任务。

      尝试为位置提供权限表单应用程序设置并检查它是否有效。

      您也应该在代码中处理棉花糖的权限。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多