0
我已經爲Google Maps API編寫了以下代碼。我在地圖上添加了標記,但每次我在導航活動中打開「地圖」時,所有標記都會消失。除此之外,我有一個RecylerView,當我在onCreate方法中使用它時,它不會從我的API中獲取數據。在片段Android Studio中創建解析器JSON的類
package com.example.sander.app;
import android.app.Fragment;
/**
* Created by Sander on 4-4-2017.
*/
import android.app.Fragment;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.Manifest;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class GoogleMaps extends Fragment implements OnMapReadyCallback {
ArrayList<String> list = new ArrayList<>();
ArrayList<Double> latitude = new ArrayList<>();
ArrayList<Double> longitude = new ArrayList<>();
public GoogleMaps(){
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_gmaps, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
String url= "http://test.dontstealmywag.ga/api/parkgarage.php";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Do something with the response
try{
JSONObject o = new JSONObject(response);
JSONArray values=o.getJSONArray("parkgarage");
for (int i=0; i< values.length(); i++) {
JSONObject jsonObject = values.getJSONObject(i);
list.add(jsonObject.getString("parkgarage_name"));
longitude.add(jsonObject.getDouble("langitude"));
latitude.add(jsonObject.getDouble("longitude"));
}
} catch (JSONException ex){}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
rq.add(stringRequest);
super.onViewCreated(view, savedInstanceState);
MapView mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mapView.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng marker = new LatLng(51.9244201, 4.4777325);
for(Integer i = 0; i < list.size(); i++){
googleMap.addMarker(new MarkerOptions().title(list.get(i)).position(new LatLng(longitude.get(i), latitude.get(i))));
}
googleMap.addMarker(new MarkerOptions().title("Testing").position(marker));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 12));
//googleMap.addMarker(new MarkerOptions().title(list.get(1)).position(marker));
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
我認爲問題出在我的JSON解析器代碼中。我想把它放在一個單獨的課上。任何人都可以幫助我嗎?