-1

i hope somebody could help me. i just lunched the test app to show up google maps on my device and now i would like to show up devices position as a marker. the position is stored in "longitude" and "latitude". how can i use them in onMapready?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Intent intent = new Intent(this,GPSTrackerActivity.class);
    startActivityForResult(intent,1);
    if(requestCode == 1){
        Bundle extras = data.getExtras();
        Double longitude = extras.getDouble("Longitude");
        Double latitude = extras.getDouble("Latitude");}}

@Override
public void onMapReady(GoogleMap googleMap) {

}
Yassin
  • 1
  • 1

2 Answers2

0

Using the Google Play services location APIs, your app can request the last known location of the user's device. What you need here is Fused Location Provider API to retrieve the device's last known location. The fused location provider is one of the location APIs in Google Play services. It manages the underlying location technology and provides a simple API so that you can specify requirements at a high level, like high accuracy or low power. It also optimizes the device's use of battery power.

To request the last known location, call the getLastLocation() method, passing it your instance of the GoogleApiClient object. Do this in the onConnected() callback provided by Google API Client, which is called when the client is ready.

public class MainActivity extends ActionBarActivity implements
        ConnectionCallbacks, OnConnectionFailedListener {
    ...
    @Override
    public void onConnected(Bundle connectionHint) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }
}

To know more about it, check this documentation and these related SO questions.

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • I have tried the solution but ActionBarActivity is deprecated – Yassin Aug 03 '16 at 20:05
  • hmmmmmm....check this [SO question](http://stackoverflow.com/questions/38619091/getting-location-of-a-direction-of-current-location-in-android/38640175#38640175), if it can help you. – KENdi Aug 04 '16 at 00:09
0

Hope this would be complete solution of your problem.

    import android.support.v4.app.FragmentActivity;
    import android.os.Bundle;

    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;


    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{

        private GoogleMap mMap;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        }

        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            LatLng yourpostion = new LatLng(0, 0);
            mMap.addMarker(new MarkerOptions().position(yourpostion));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(yourpostion));
        }
    }

     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent           data) {
        super.onActivityResult(requestCode, resultCode, data);
        Intent intent = new Intent(this,GPSTrackerActivity.class);
        startActivityForResult(intent,1);
        if(requestCode == 1){
           Bundle extras = data.getExtras();
           Double longitude = extras.getDouble("Longitude");
           Double latitude = extras.getDouble("Latitude");
           LatLng yourpostion = new LatLng(latitude, longitude);
           mMap.addMarker(new MarkerOptions().position(yourpostion));
           mMap.moveCamera(CameraUpdateFactory.newLatLng(yourpostion)); 
     }
    }
  • khalil, your solution didnt work for me. i dont know how to use the values (Latitude and Longitude) from onconnected in onMapReady :-( – Yassin Aug 03 '16 at 20:07