-1

What I am trying to do is to make a google maps app in which I can move the map and the marker position stays centered (DONE), something like Uber. Then after the push of a button (buttonGPS) get the lat (tvValueLatitude) and long (tvValueLatitude) coordinates to display in two textview (DONE). I need help hidding the blue dot while leaving the button in the top right, also getting the coordinates from where I put the grey marker at when moving the map. enter image description here Next is the xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">


    <RelativeLayout
        android:id="@+id/rlmap"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/rlData">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <fragment xmlns:android="http://schemas.android.com/apk/res/android"

                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:id="@+id/map_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@drawable/pin"
                />


        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rlData"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingBottom="0dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true">




        <LinearLayout
            android:id="@+id/llGPS"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_marginTop="@dimen/activity_horizontal_margin"
            android:divider="?android:dividerHorizontal"
            android:orientation="horizontal"
            android:showDividers="middle">

            <Button
                android:id="@+id/buttonGPS"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Get GPS" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_horizontal_margin"
                android:layout_weight="1"
                android:divider="?android:dividerVertical"
                android:orientation="vertical"
                android:showDividers="middle">

                <TextView

                    android:id="@+id/tvLatitude"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/Latitude"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/Black"

                    />

                <TextView

                    android:id="@+id/tvValueLatitude"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/activity_vertical_margin"
                    android:gravity="center"
                    android:hint="@string/valueLatitude"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/Black"
                    android:textColorHint="@color/Gray" />


            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_horizontal_margin"
                android:layout_weight="1"
                android:divider="?android:dividerVertical"
                android:orientation="vertical"
                android:showDividers="middle">

                <TextView

                    android:id="@+id/tvLongitude"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/Longitude"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/Black"

                    />

                <TextView

                    android:id="@+id/tvValueLongitude"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/activity_vertical_margin"
                    android:gravity="center"
                    android:hint="@string/valueLongitude"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/Black"
                    android:textColorHint="@color/Gray" />


            </LinearLayout>

        </LinearLayout>
    </RelativeLayout>

The main activity

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{

    private GoogleMap mMap;
    TextView lat,longt;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        lat=findViewById(R.id.tvValueLongitude);
        longt=findViewById(R.id.tvValueLatitude);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney, Australia, and move the camera.
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }




}

So far I just have the map. Any help with coding or pointing a good tutorial would be great, thanks!

  • Put the `SupportMapFragment` in a container that supports Z-axis ordering (e.g., `ConstraintLayout`). Put your marker in that same container, centered over the map (note: this will be much easier if your marker has its "point" in the center of the image). Now, when the user pans the map, all you need to do is get the coordinates for the center of the map, which you can get from `getCameraPosition()` on `GoogleMap`. – CommonsWare Dec 05 '18 at 00:15
  • Take a look at [this](https://stackoverflow.com/a/40113749/6950238) answer. – Andrii Omelchenko Dec 05 '18 at 09:59
  • Possible duplicate of [Android: Drag map while keeping marker at the center](https://stackoverflow.com/questions/40112760/android-drag-map-while-keeping-marker-at-the-center) – Andrii Omelchenko Dec 05 '18 at 10:01

1 Answers1

0

If I understand your question, this is what you need:

Global var: private Marker marker;

Put start marker on center of the screen:

    public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            mMap.setOnCameraMoveListener(this);
            LatLng center = mMap.getCameraPosition().target;
            marker = mMap.addMarker(new MarkerOptions().position(mMap.getCameraPosition().target));
    }

Change it position:

@Override
public void onCameraMove() {
    LatLng center = mMap.getCameraPosition().target;
    marker.setPosition(center);
}

And onClick to get current LatLng - LatLng markerPosition = marker.getPosition();

Vadim Eksler
  • 865
  • 9
  • 24