如何在google map v2中获取标记的经度和纬度值

时间:2014-10-08 10:54:32

标签: java android eclipse google-maps

我在谷歌地图v2工作。我刚在地图上添加了一个标记。但我无法从标记中获取位置。我在谷歌搜索了很多但没有得到正确的答案。我只想烘烤标记的经度和纬度值。谁能建议我什么?

我的代码:

double mylongitudey,mylatitudey;
String latilongi;
ImageView iv;
Button manual,proceed;

GoogleMap map;
LatLng mylat ;

String latlong;
Marker mymarker,marker;
boolean markerclicked;
double mylatitudeyd,mylongitudeyd;
String addstringaa;


@Override
protected void onCreate(Bundle savedInstanceState) {
 LocationManager lm;
  lm= (LocationManager) getSystemService(Context.LOCATION_SERVICE);

  Toast.makeText(this,"Please select the destination by long click", Toast.LENGTH_LONG).show();

  LatLng mylat;

  final GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
  map.setOnMarkerDragListener(this);
  map.setTrafficEnabled(true);
  map.setMyLocationEnabled(true);
  map.setOnMarkerDragListener(this);


  map.setOnMyLocationButtonClickListener(this);

  map.animateCamera(CameraUpdateFactory.zoomTo(15));
 // map.setOnMyLocationButtonClickListener(this);


  map.moveCamera(CameraUpdateFactory.newLatLngZoom(mylat, 13));
 map.addMarker(new MarkerOptions().position(mylat).title("You are Here"));

 markerclicked=false;
  //onMyLocationButtonClick();
  map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

    @Override
    public void onMapLongClick(LatLng arg0) {
        // TODO Auto-generated method stub


        if(markerclicked==false)
        {
         mymarker = map.addMarker(new MarkerOptions().position(arg0).title("Your Destination").draggable(true));
         markerclicked=true;
    }else{

         Toast.makeText(getApplicationContext(), "Marker Aready Set..!", Toast.LENGTH_LONG).show();

    }

    }
});


  map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
    LatLng mylatlong;
    @Override
    public void onMyLocationChange(Location arg0) {

        // TODO Auto-generated method stub
        mylongitudey = arg0.getLongitude();
        mylatitudey = arg0.getLatitude();
        mylatlong = new LatLng(arg0.getLatitude(), arg0.getLongitude());
        //sourcepoint=String.format(" ",mylat);
        marker= map.addMarker(new MarkerOptions().position(mylatlong).title("YOur Destination"));
//  map.moveCamera(CameraUpdateFactory.newLatLngZoom(mylat,1));
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(mylatlong, 14));


    }
});

3 个答案:

答案 0 :(得分:2)

调用marker.getPosition()将返回存储纬度和经度的LatLng对象。

Marker marker = <your initialization>;
LatLng latLng = marker.getPosition();
double latitude = latLng.latitude;
double longitude = latLng.longitude;

答案 1 :(得分:0)

try {
            List<Address> detail = gcd.getFromLocation(location.getLatitude(),
                    location.getLongitude(), 1);
            cityName = detail.get(0).getAddressLine(1);

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

latitude = detail.get(0).getLatitude();

答案 2 :(得分:0)

Log.d("location"+marker.getPosition().longitude+" "+marker.getPosition().latitude);

可能这会帮助......