Get Latitude and longitude of marker in google maps
Asked Answered
F

2

11

I'm using the following code to create a map and attach a marker to it. I'm also adding a marker listener where I need to get the longitude and latitude of the marker position after dragging.

What it does is returnning my current location not the location of the marker after drag. Any help with this part?!

package com.example.mysample;

import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
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 com.google.android.maps.GeoPoint;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.Toast;
import android.graphics.*;

public class MainActivity extends FragmentActivity implements LocationListener {

    GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getApplicationContext());
        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
        map.setMyLocationEnabled(true);
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.addMarker(new MarkerOptions()
                .position(new LatLng(0, 0))
                .title("Marker")
                .draggable(true)
                .snippet("Hello")
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));

        map.setOnMarkerDragListener(new OnMarkerDragListener() {

            @Override
            public void onMarkerDragStart(Marker marker) {
                // TODO Auto-generated method stub
                // Here your code
                Toast.makeText(MainActivity.this, "Dragging Start",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onMarkerDragEnd(Marker marker) {
                // TODO Auto-generated method stub
                Toast.makeText(
                        MainActivity.this,
                        "Lat " + map.getMyLocation().getLatitude() + " "
                                + "Long " + map.getMyLocation().getLongitude(),
                        Toast.LENGTH_LONG).show();
                System.out.println("yalla b2a "
                        + map.getMyLocation().getLatitude());
            }

            @Override
            public void onMarkerDrag(Marker marker) {
                // TODO Auto-generated method stub
                // Toast.makeText(MainActivity.this, "Dragging",
                // Toast.LENGTH_SHORT).show();
                System.out.println("Draagging");
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean onMarkerClick(final Marker marker) {

        if (marker.equals(map)) {
            // handle click here
            // map.getMyLocation();
            System.out.println("Clicked");
            double lat = map.getMyLocation().getLatitude();
            System.out.println("Lat" + lat);
            Toast.makeText(MainActivity.this,
                    "Current location " + map.getMyLocation().getLatitude(),
                    Toast.LENGTH_SHORT).show();
        }
        return true;
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}
Foregoing answered 24/4, 2013 at 1:36 Comment(1)
In the javascript API all MouseEvents contain the latLng of the point on the map where the event occurred, API reference not sure how you access this in Java though. In your onDragEnd function can't you read the position of the marker? Something like marker.getLocation().getLongitude() and marker.getLocation().getLatitude()Antiserum
S
34

Take a look at this function in your code.

   @Override
    public void onMarkerDragEnd(Marker marker) {
        // TODO Auto-generated method stub
        Toast.makeText(
                MainActivity.this,
                "Lat " + map.getMyLocation().getLatitude() + " "
                        + "Long " + map.getMyLocation().getLongitude(),
                Toast.LENGTH_LONG).show();
        System.out.println("yalla b2a "
                + map.getMyLocation().getLatitude());
    }

Here you are trying to get your current location on map which is wrong you should get location of marker that you dragged. You already have "marker" object here. Use that to get location of this draged marker's location.

LatLng position = marker.getPosition(); //
Toast.makeText(
                MainActivity.this,
                "Lat " + position.latitude + " "
                        + "Long " + position.longitude,
                Toast.LENGTH_LONG).show();
Saturable answered 24/4, 2013 at 2:48 Comment(1)
Hi @Sharj I have a small problem in comparing the latitude and longitude of map marker. When i am adding a marker i am passing the co-ordinates (25.197109,55.279104) but when i call marker.getPosition() it is giving me (25.197101045,55.279104546). I think it is giving the exact and accurate lat lng, as a result i am not able to compare the that lat lng with the one i have. Please suggest me any solution ..Adept
O
7

It s quite simple i think if you want the long and lat coordinates with a long press ...

First, you have to do GoogleMap.setOnMapLongClickListener(this); and add to the signature of the containing class: implements OnMapLongClickListener

And here is the code:

@Override    
public void onMapLongClick(LatLng point) { 
    Toast.makeText(MainActivity.this, point.latitude+" "+point.longitude, Toast.LENGTH_SHORT).show();

}
Optime answered 18/1, 2014 at 18:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.