11

我正在使用以下代码创建地图并为其附加标记。我还添加了一个标记侦听器,我需要在拖动后获取标记位置的经度和纬度。

它所做的是在拖动后返回我当前的位置而不是标记的位置。这部分有什么帮助吗?!

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

    }

}
4

2 回答 2

34

看看你的代码中的这个函数。

   @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());
    }

在这里,您试图在地图上获取当前位置,这是错误的,您应该获得拖动的标记的位置。您在这里已经有了“标记”对象。使用它来获取此拖动标记位置的位置。

LatLng position = marker.getPosition(); //
Toast.makeText(
                MainActivity.this,
                "Lat " + position.latitude + " "
                        + "Long " + position.longitude,
                Toast.LENGTH_LONG).show();
于 2013-04-24T02:48:55.990 回答
7

我想这很简单,如果你想要长按的经纬坐标...

首先,您必须GoogleMap.setOnMapLongClickListener(this); 添加到包含类的签名中:implements OnMapLongClickListener

这是代码:

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

}
于 2014-01-18T18:36:25.660 回答