ali
ali

Reputation: 29

How do I get the Latitudes and Longitudes when I click a marker displayed in the google map

I have implemented a way to search a place from the search bar and the application displays the marker absolutely fine. But after I click the marker which is displayed no toast messages appear. Could someone show me how to implement the same. Here is the code that I used, but I simply can't get the latitude and longitude when I click on a marker.

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

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

public class SearchMaps extends FragmentActivity implements OnMapReadyCallback,GoogleMap.OnMarkerClickListener {
private GoogleMap mMap;

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

public void onSearch(View view) {
    EditText location_tf = (EditText) findViewById(R.id.TSearch);
    String location = location_tf.getText().toString();
    List<Address> addressList = null;

    if (location != null || !location.equals("")) {
        Geocoder geocoder = new Geocoder(this);
        try {
            addressList = geocoder.getFromLocationName(location, 1);

        } catch (IOException e) {
            e.printStackTrace();
        }

        android.location.Address address = addressList.get(0);
        LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
        mMap.clear();
        mMap.addMarker(new MarkerOptions().position(latLng).title("Marker")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    }
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


}
@Override
public boolean onMarkerClick(Marker marker) {
    LatLng position = marker.getPosition();
    Toast.makeText(
            SearchMaps.this,
            "Lat " + position.latitude + " " + "Long " + position.longitude,Toast.LENGTH_LONG).show();
    return false;
}
}

Upvotes: 0

Views: 49

Answers (2)

antonio
antonio

Reputation: 18262

Just add mMap.setOnMarkerClickListener(this); at the end of your onMapReady method.

Upvotes: 1

Thibaud Renaux
Thibaud Renaux

Reputation: 280

Did you try first using Log.d ? Because Toast need a context and sometimes you need to attach a Toastto the UI using runOnUIThread

Upvotes: 0

Related Questions