Search code examples
androidgoogle-mapsparse-platform

Android How to create a map marker using a ParseGeoPoint


I have a GeoPoint stored in a parse object called "Request" but I'm not sure how to retrieve the GeoPoint SUCCESSFULLY to pass it to the LatLng object. I have looked at the documentation but it hasn't helped me in this instance. The problem seems to be that in findInBackGround() where the geoPoint can be retrieved from the list of Request objects does not allow me to access the geoPoint in the onMap method. I have been struggling with this for a while maybe I'm missing something here is the class where I'm trying to achieve this:

import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.List;

public class ViewMapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    LatLng businessLocation;
    ParseQuery request;
    ParseGeoPoint geoPoint;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_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;


        request = ParseQuery.getQuery("Request");
        request.whereEqualTo("businessName", ParseUser.getCurrentUser().getUsername());
        request.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {

                if (e == null) {
                    if (objects.size() > 0) {

                        geoPoint = objects.get(0).getParseGeoPoint("GeoPoint");

                        } else {
                            Log.i("Map", "Map not displayed!");
                        }
                    }

                }

            }
        });
      if (businessLocation != null) {

                            businessLocation = new LatLng(-34, 20);
                            mMap.addMarker(new MarkerOptions().position(businessLocation).title("Your Location"));
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(businessLocation, 15));

      }
    }

Solution

  • I was able to figure it out, i don't know what was causing the problem but removing if(businessLocation != null) allowed the LatLng to be populated.

     @Override
        public void onMapReady(final GoogleMap googleMap) {
            mMap = googleMap;
    
    
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Request");
            query.whereEqualTo("businessName", ParseUser.getCurrentUser().getUsername());
            query.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> objects, ParseException e) {
                    if (e == null) {
                        if (objects.size() > 0) {
    
                                    geoPoint = objects.get(0).getParseGeoPoint("GeoPoint");
    
                                            businessLocation = new LatLng(geoPoint.getLatitude(), geoPoint.getLongitude());
    
                                            //mMap.clear();
                                            mMap.addMarker(new MarkerOptions().position(businessLocation).title("Your Location!"));
                                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(businessLocation, 9));
    
                                    } else {
                                        Toast.makeText(ViewMapsActivity.this, "No objects found.", Toast.LENGTH_SHORT).show();
                                    }
    
                        }
                    }
    
            });
    
    
    
        }