我正在通过从Foursquare获取场地并使用Google Map创建一个Android地图应用。

我已经将MainActivity设置为获取场地结果,MapFragmentClass和VenueModel。

我将所有来自Foursquare的JSON结果保存到MainActivity中的List placeModelList中。

我想要做的是基于从Foursquare接收到的坐标在MapFragmentClass中添加标记。

但是,我一直试图将venueModelList传递给MapFragmentClass。

任何帮助表示赞赏。谢谢。

MainActivity.java

public class MainActivity extends AppCompatActivity implements LocationListener{
private final String VENUE_URL = "https://api.foursquare.com/v2/venues/search?ll=";

private final int LIMIT = 40;

private final double RADIUS = 50000;

private String MOSQUE_URL;
private String RES_URL;

public static final String CLIENT_ID = "";
public static final String CLIENT_SECRET = "";

private static final long MIN_TIME_BW_UPDATES = 20000;
private static final float MIN_DISTANCE_CHANGE_FOR_UPDATES = 1;
private LocationManager locationManager;
private Location lastLocation;
private Location location;
private boolean receivedLocation = false;
private double lt;
private double lg;
private boolean canGetLocation;
private boolean isGPSEnabled;
private boolean isNetworkEnabled;
private boolean updateSettings = false;
private String TAG = "TAG";
private ListView lvVenues;
private Bundle bundle;
private ArrayList<VenueModel> venueModelList;


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lvVenues =  (ListView) findViewById(R.id.lvVenues);

    String t = timeMilisToString(System.currentTimeMillis());

    Bundle extras = getIntent().getExtras();
    if (extras != null)
    {
        lt = extras.getDouble("LATITUDE");
        lg = extras.getDouble("LONGITUDE");
        receivedLocation = true;
    }
    else
    {
        receivedLocation = false;
    }

    location = getLocation();
    if (location != null)
    {
        if(receivedLocation)
        {
            location.setLatitude(lt);
            location.setLongitude(lg);
        }
        else
        {
            lt = location.getLatitude();
            lg = location.getLongitude();
            Log.d("LAT", "Latitude: " + lt);
            Log.d("LONG", "Longitude: " + lg);
        }
    }

    double lt = 3.142182;
    double lg = 101.710602;

    MOSQUE_URL = VENUE_URL + lt + "," + lg
            + "&client_id=" + CLIENT_ID
            + "&client_secret=" + CLIENT_SECRET
            + "&v=" + t
            + "&categoryId=4bf58dd8d48988d138941735"
            + "&radius=" + RADIUS
            + "&limit=" + LIMIT;

    RES_URL = VENUE_URL + lt + "," + lg
            + "&client_id=" + CLIENT_ID
            + "&client_secret=" + CLIENT_SECRET
            + "&v=" + t
            + "&categoryId=52e81612bcbc57f1066b79ff"
            + "&radius=" + RADIUS
            + "&limit=" + LIMIT;
}

public class JSONTask extends AsyncTask<String, String, List<VenueModel>>
{
    @Override
    public List<VenueModel> doInBackground(String... params)
    {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            String finalJson = buffer.toString();

            JSONObject parentObject = new JSONObject(finalJson);
            JSONObject secondObject = parentObject.getJSONObject("response");
            JSONArray parentArray = secondObject.getJSONArray("venues");

            venueModelList = new ArrayList<>();

            for (int i = 0; i < parentArray.length(); i++)
            {
                JSONObject finalObject = parentArray.getJSONObject(i);
                JSONObject thirdObject = finalObject.getJSONObject("location");

                try {
                    VenueModel venueModel = new VenueModel();
                    venueModel.setId(finalObject.getString("id"));
                    venueModel.setName(finalObject.getString("name"));
                    venueModel.setAddress(thirdObject.optString("address"));
                    venueModel.setPostalCode(thirdObject.optString("postalCode"));
                    venueModel.setCity(thirdObject.optString("city"));
                    venueModel.setState(thirdObject.optString("state"));
                    venueModel.setDistance(thirdObject.getInt("distance"));
                    venueModel.setLat(thirdObject.getDouble("lat"));
                    venueModel.setLng(thirdObject.getDouble("lng"));
                    LatLng coordinate = new LatLng(thirdObject.getDouble("lat"), thirdObject.getDouble("lng"));
                    venueModel.setCoordinate(coordinate);

                    venueModelList.add(venueModel);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

            Collections.sort(venueModelList);

            return venueModelList;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(List<VenueModel> result)
    {
        super.onPostExecute(result);
        VenueAdapter adapter = new VenueAdapter(getApplicationContext(), R.layout.row, result);
        lvVenues.setAdapter(adapter);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    int id = item.getItemId();

    if (id == R.id.action_mosque)
    {
        new JSONTask().execute(MOSQUE_URL);
        FragmentManager fmanager = getSupportFragmentManager();
        FragmentTransaction FT = fmanager.beginTransaction();
        MapFragmentClass mfc = new MapFragmentClass();
        FT.add(R.id.mapLayout, mfc);
        FT.commit();

        return true;
    }

    if (id == R.id.action_restaurant)
    {
        new JSONTask().execute(RES_URL);

        FragmentManager fmanager = getSupportFragmentManager();
        FragmentTransaction FT = fmanager.beginTransaction();
        MapFragmentClass mfc = new MapFragmentClass();
        FT.add(R.id.mapLayout, mfc);
        FT.commit();

        return true;
    }

    return super.onOptionsItemSelected(item);
}

private String timeMilisToString(long milis)
{
    SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(milis);
    return sd.format(calendar.getTime());
}

public Location getLocation()
{
    try
    {
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled)
        {
            Log.v(TAG, "No network provider enabled");
        }
        else
        {
            this.canGetLocation = true;
            if (isNetworkEnabled)
            {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d(TAG, "Network Enabled");

                if(locationManager != null)
                {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                }
            }

            if (isGPSEnabled)
            {
                if (location == null)
                {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d(TAG, "GPS Enabled");

                    if(locationManager != null)
                    {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    }
                }
            }
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return location;
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
public void onLocationChanged(Location location)
{
    lastLocation = location;
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider)
{
    Log.v(TAG, "onProviderEnabled");
}

@Override
public void onProviderDisabled(String provider)
{
    Log.v(TAG, "onProviderDisabled");
}

public LatLng setCoordinate()
{
    LatLng coordinate = new LatLng(getLocation().getLatitude(), getLocation().getLongitude());
    LatLng coordinate = new LatLng(lt, lg);
    return coordinate;
}
}


VenueModel.java

public class VenueModel implements Comparable<VenueModel>, Parcelable
{
private String id;
private String name;
private String address;
private String postalCode;
private String city;
private String state;
private Integer distance;
private double lat;
private double lng;
private LatLng coordinate;
private List<VenueModel> venueModelList;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public int getDistance() {
    return distance;
}

public void setDistance(int distance) {
        this.distance = distance;
}

public double getLat() {
    return lat;
}

public void setLat(double lat) {
    this.lat = lat;
}

public double getLng() {
    return lng;
}

public void setLng(double lng) {
    this.lng = lng;
}

public String getPostalCode() {
    return postalCode;
}

public void setPostalCode(String postalCode) {
    this.postalCode = postalCode;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public void setVenueModelList(List<VenueModel> venueModelList)
{
    this.venueModelList = venueModelList;
}

public List<VenueModel> getVenueModelList()
{
    return venueModelList;
}

public LatLng getCoordinate() {
    return coordinate;
}

public void setCoordinate(LatLng coordinate)
{
    this.coordinate = coordinate;
}

@Override
public int compareTo(VenueModel venueModel) {

    return this.distance.compareTo(venueModel.distance);
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.id);
    dest.writeString(this.name);
    dest.writeString(this.address);
    dest.writeString(this.postalCode);
    dest.writeString(this.city);
    dest.writeString(this.state);
    dest.writeValue(this.distance);
    dest.writeDouble(this.lat);
    dest.writeDouble(this.lng);
    dest.writeParcelable(this.coordinate, flags);
    dest.writeList(this.venueModelList);
}

public VenueModel() {
}

protected VenueModel(Parcel in) {
    this.id = in.readString();
    this.name = in.readString();
    this.address = in.readString();
    this.postalCode = in.readString();
    this.city = in.readString();
    this.state = in.readString();
    this.distance = (Integer) in.readValue(Integer.class.getClassLoader());
    this.lat = in.readDouble();
    this.lng = in.readDouble();
    this.coordinate = in.readParcelable(LatLng.class.getClassLoader());
    this.venueModelList = new ArrayList<VenueModel>();
    in.readList(this.venueModelList, VenueModel.class.getClassLoader());
}

public static final Parcelable.Creator<VenueModel> CREATOR = new Parcelable.Creator<VenueModel>() {
    @Override
    public VenueModel createFromParcel(Parcel source) {
        return new VenueModel(source);
    }

    @Override
    public VenueModel[] newArray(int size) {
        return new VenueModel[size];
    }
};
}


MapFragmentClass.java

public class MapFragmentClass extends Fragment implements OnMapReadyCallback
{
private MainActivity mA;
private GoogleMap gmap;
private static final LatLng coordinate = new LatLng(3.152182, 101.710602);
private LatLng coordinate2;
private ArrayList<VenueModel> venueModelList;


@Override
public void onAttach(Context context)
{
    super.onAttach(context);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View v = inflater.inflate(R.layout.activity_maps, container, false);
    SupportMapFragment supportMapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
    supportMapFragment.getMapAsync(this);

    return v;
}

@Override
public void onMapReady(GoogleMap googleMap)
{
    gmap = googleMap;
    gmap.addMarker(new MarkerOptions().position(coordinate).title("Your location").draggable(true));
    gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 14));
}


}

最佳答案

使用GSON将该对象转换为JSON字符串,然后再次将该字符串再次转换为list的对象。

MapFragment.getInstance(new Gson().toJson(venuList));

public static final getInstance(String venusStringListObj) {}
Bundle argument = new Bundle();
argument.putString("VENUES", venusStringListObj);
MapFragment fragment = new MapFragment();
fragment.setArgument(argument);
return fragment;
}

onCreate(...) {
    private List<VenueModel> venueList;
 Type listType = new TypeToken<ArrayList<VenueModel>>() {}.getType();
String venueStringRecvFromFragArg = getArguments().getString("VENUES");
            venueList = new Gson().fromJson(venueStringRecvFromFragArg, listType);
}

10-08 18:00