0

At the moment I am having a hard time placing the marker on the map . I want to do an app where I track the path I took while walking. Can you help me with a link to accomplish this and how do I place the marker in the map given the previous code?.

i followed the parse guide in at https://www.back4app.com/docs/android/parse-geopoint but this doesnt seem to work.

should be placed in Colombia South America

this is what I have done so far...

package com.example.mac.mycarapp.Fragment;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.example.mac.mycarapp.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.parse.ParseGeoPoint;
import com.parse.ParseUser;

public class PruebaDeRutaMapsActivity extends FragmentActivity implements OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {

    private GoogleMap mMap;
    private GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    LocationRequest mLocationRequest;
    private static final int REQUEST_LOCATION = 1;
    LocationManager locationManager;

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

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);



    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

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

        buildGoogleApiClient();
        mMap.setMyLocationEnabled(true);

        ParseGeoPoint loc=getCurrentUserLocation();

        // Add a marker in Sydney and move the camera
       showCurrentUserInMap(googleMap);

    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient=new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
    }

    @Override
    public void onLocationChanged(Location location) {

        mLastLocation=location;


        LatLng latLng=new LatLng(location.getLatitude(),location.getLongitude());
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        /** Jugar con el valor de ZoomTo para obtener el valor deseado va de 1 a 21*/
        mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

        //TODO:Agregar la localizacion a la base de datos

        //https://www.back4app.com/docs/android/parse-geopoint


        if(location != null){
            // if it isn't, save it to Back4App Dashboard

            ParseUser currentUser = ParseUser.getCurrentUser();

            if (currentUser != null) {
                saveCurrentUserLocation();

            } else {
                // do something like coming back to the login activity
            }
        }
        else {
            // if it is null, do something like displaying error and coming back to the menu activity
        }



    }

    /** When the map is connected and the location is being requested*/
    @Override
    public void onConnected(@Nullable Bundle bundle) {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        /**
         * OJO si esta puesto en high drenara mas rapido la bateria del dispositivo*/
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        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;
        }

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    private void saveCurrentUserLocation() {
        // requesting permission to get user's location
        if (ActivityCompat.checkSelfPermission(PruebaDeRutaMapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(PruebaDeRutaMapsActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(PruebaDeRutaMapsActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
        } else {
            // getting last know user's location
            Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            // checking if the location is null
            if (location != null) {
                // if it isn't, save it to Back4App Dashboard
                ParseGeoPoint currentUserLocation = new ParseGeoPoint(location.getLatitude(), location.getLongitude());

                ParseUser currentUser = ParseUser.getCurrentUser();

                if (currentUser != null) {
                    currentUser.put("Location", currentUserLocation);
                    currentUser.saveInBackground();
                } else {
                    // do something like coming back to the login activity
                }
            } else {
                // if it is null, do something like displaying error and coming back to the menu activity
            }
        }
    }
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
        {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);

            switch (requestCode) {
                case REQUEST_LOCATION:
                    saveCurrentUserLocation();
                    break;
            }
        }
    private ParseGeoPoint getCurrentUserLocation(){

        // finding currentUser
        ParseUser currentUser = ParseUser.getCurrentUser();

        if (currentUser == null) {
            // if it's not possible to find the user, do something like returning to login activity
        }
        // otherwise, return the current user location
        return currentUser.getParseGeoPoint("Location");

    }

    private void showCurrentUserInMap(final GoogleMap googleMap){

        // calling retrieve user's location method of Step 4
        ParseGeoPoint currentUserLocation = getCurrentUserLocation();

        // creating a marker in the map showing the current user location
        LatLng currentUser = new LatLng(currentUserLocation.getLatitude(), currentUserLocation.getLongitude());
        googleMap.addMarker(new MarkerOptions().position(currentUser).title(ParseUser.getCurrentUser().getUsername()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

        // zoom the map to the currentUserLocation
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentUser, 5));
    }
    }
Felipe Franco
  • 171
  • 3
  • 15
  • Why do you want to use ParseGeoPoint since you can have coordinates with LatLng or Location object? – Turvy Jun 30 '19 at 21:23
  • check this question it may help you :https://stackoverflow.com/questions/41500765/how-can-i-get-continuous-location-updates-in-android-like-in-google-maps – ismail alaoui Jul 01 '19 at 00:00
  • Because i need to track the current location every second and trace where i walk – Felipe Franco Jul 01 '19 at 17:58

1 Answers1

0

You need to make sure that you are updating the map every time that you have a new location. Try to change this method:

    @Override
    public void onLocationChanged(Location location) {

        mLastLocation=location;


        LatLng latLng=new LatLng(location.getLatitude(),location.getLongitude());
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        /** Jugar con el valor de ZoomTo para obtener el valor deseado va de 1 a 21*/
        mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

        //TODO:Agregar la localizacion a la base de datos

        //https://www.back4app.com/docs/android/parse-geopoint


        if(location != null){
            // if it isn't, save it to Back4App Dashboard

            ParseUser currentUser = ParseUser.getCurrentUser();

            if (currentUser != null) {
                saveCurrentUserLocation();
                showCurrentUserInMap(mMap);

            } else {
                // do something like coming back to the login activity
            }
        }
        else {
            // if it is null, do something like displaying error and coming back to the menu activity
        }



    }
Davi Macêdo
  • 2,954
  • 1
  • 8
  • 11
  • hello davi i added the showCurrentUserInMap(mMap); but still doesnt work! do you have any more ideas? maybe im missing something else? About the walk app i want to develop do you have any documentation i need to trace the route in the map which where i walk. – Felipe Franco Jul 01 '19 at 20:41
  • Can you add a breakpoint in the line `showCurrentUserInMap(mMap);` and check if the program is getting there? – Davi Macêdo Jul 01 '19 at 20:48
  • hello davi the program didnt get there! and also the location marker doesnt appear in the map – Felipe Franco Jul 01 '19 at 20:52
  • Is it at least getting into the function? Does it stop in `mLastLocation=location;` ? – Davi Macêdo Jul 02 '19 at 16:10
  • no davi it doesnt even run maybe because im not moving? or it should run – Felipe Franco Jul 02 '19 at 20:12
  • Yes. You will have to move in order to fire this method. – Davi Macêdo Jul 03 '19 at 16:41
  • If you never moved, you never collected any location and there is not geo point in your database to be marked yet. You should add some code to collection the initial location. It should be something like this: https://www.back4app.com/docs/android/parse-geopoint#step-3---save-users-current-location-in-your-back4pp-dashboard – Davi Macêdo Jul 04 '19 at 06:39
  • thank you davi i didnt noticed this i will try it in my code and let you know – Felipe Franco Jul 04 '19 at 22:48