hippoman
hippoman

Reputation: 330

How can I get the location, as a string, of a map marker in Android studio?

I am trying to get the current location of the marker from a location-based-services app and then display that location as a string. What classes do I need to call? I know I can get the map variable as a GoogleMap, but I'm not sure which functions to call to get the latitude and longitude and then convert that into a string.

Thanks in advance!

Here is my code so far. I am trying to put it in displayCurrentLocation to display the current location as a toast:

package com.example.lbs;

import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
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.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private MarkerOptions mo;

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


    /**
     * 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;

        // Add a marker in Sydney 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));

        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng point) {
                Log.d("DEBUG","Map clicked [" + point.latitude + " / " + point.longitude + "]");

                Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
                try {
                    List<Address> addresses = geoCoder.getFromLocation(point.latitude,point.longitude,1);
                    String add = "";
                    if (addresses.size() > 0)
                    {
                        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
                            add += addresses.get(0).getAddressLine(i) + "\n";
                    }
                    Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }

                LatLng p = new LatLng(point.latitude, point.longitude);
                mMap.moveCamera(CameraUpdateFactory.newLatLng(p));
                mMap.clear();
                mMap.addMarker(new MarkerOptions().position(point));
                //mMap.addMarker(mo.position(point));
            }
        });
    }

    public void showMapView(View view){
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }
    public void showSatView(View view){
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    }
    public void findLocation(View view){
        Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
        EditText location_et = (EditText) findViewById(R.id.location);
        String location = location_et.getText().toString();
        try {
            List<Address> addresses = geoCoder.getFromLocationName(
                    location, 5);
            if (addresses.size() > 0) {
                LatLng p = new LatLng((int) (addresses.get(0).getLatitude()),
                        (int) (addresses.get(0).getLongitude()));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(p));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void displayCurrentLocation(View view){
        //Location loc = mMap.getMyLocation();
        //String location = loc.toString();
        //Toast.makeText(getBaseContext(),location, Toast.LENGTH_SHORT);
        //Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    }


}

Upvotes: 0

Views: 1575

Answers (1)

Brian
Brian

Reputation: 56

You can get a marker's latitude and longitude as strings like this:

LatLng pos = yourMarker.getPosition();
String latitude = String.valueOf(pos.latitude);
String longitude = String.valueOf(pos.longitude);

Also, here is the documentation for the Marker class.

Upvotes: 1

Related Questions