Android标记没有显示asynctask地图

时间:2017-12-19 00:44:00

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

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

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

    }

}
}

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

只需在app gradle中设置target_sdk 22即可查看。

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

答案 2 :(得分:0)

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

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

您也应该在代码中处理marshmallow的权限。