从当前位置获取 LatLon 到标记

Get LatLon from current Position to Marker

我尝试在我的地图上放置一条从当前位置到标记的折线。 如果我将纬度和经度直接放在代码中,则折线正在绘制。 但是,如果我尝试从 Locationlistener 获取 Lat 和 Lon,它不起作用并且 googlemaps link 看起来像这样:http://maps.googleapis.com/maps/api/directions/xml?origin=0.0, 0.0&destination=0.0, 0.0...

代码如下:

private GoogleMap myMap;
private final long zero = 0;
private HashMap<String, Spot> spots = null;
private Dialog dialog;
private double sourceLatitude;
private double sourceLongitude;
private double destLatitude;
private double destLongitude;
Button buttonRequest;
GoogleDirection gd;
Document mDoc;
LatLng start = new LatLng(sourceLatitude, sourceLongitude);
LatLng end = new LatLng(destLatitude, destLongitude);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.karte);

    Dialog b = new Dialog(this);
    dialog = b;

    SupportMapFragment mySupportMapFragment =  (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
       myMap = mySupportMapFragment.getMap();
       myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
       myMap.setMyLocationEnabled(true);


    LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 0,
        mlocListener);
        mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 0, 
        mlocListener);

       SharedPreferences settings = getSharedPreferences("cityhallprefs", 0);
       moveCamera(Double.longBitsToDouble(settings.getLong("camlat", zero)),
               Double.longBitsToDouble(settings.getLong("camlng", zero)));

gd = new GoogleDirection(this);
gd.setOnDirectionResponseListener(new OnDirectionResponseListener() {
public void onResponse(String status, Document doc, GoogleDirection gd) {
    mDoc = doc;
    myMap.addPolyline(gd.getPolyline(doc, 3, Color.RED));
    myMap.addMarker(new MarkerOptions().position(start)
    .icon(BitmapDescriptorFactory.defaultMarker(
    BitmapDescriptorFactory.HUE_GREEN)));
    myMap.addMarker(new MarkerOptions().position(end)
    .icon(BitmapDescriptorFactory.defaultMarker(
    BitmapDescriptorFactory.HUE_GREEN)));
    }
    });
buttonRequest = (Button)findViewById(R.id.routereq);
buttonRequest.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
       v.setVisibility(View.GONE);
       gd.setLogging(true);
       gd.request(start, end, GoogleDirection.MODE_DRIVING);
       }
       });

    new QuerySpotsOperation().execute("");
}

@Override
protected void onPause() {
    super.onPause();
    SharedPreferences settings = getSharedPreferences("cityhallprefs", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putLong("camlat", Double.doubleToLongBits(myMap.getCameraPosition().target.latitude));
    editor.putLong("camlng", Double.doubleToLongBits(myMap.getCameraPosition().target.longitude));
    editor.commit();
}

private void moveCamera(double latit, double longit) {
    if(latit!= 0 || longit != 0) {
        LatLng coordinate = new LatLng(latit, longit);
        CameraUpdate updt = CameraUpdateFactory.newLatLngZoom(coordinate, 6);
        myMap.animateCamera(updt);
    }
}



   class MyLocationListener implements LocationListener {
      public void onLocationChanged(Location loc) {
        sourceLatitude = loc.getLatitude();
        sourceLongitude = loc.getLongitude();
        }
        public void onProviderDisabled(String provider) {
        }
        public void onProviderEnabled(String provider) {
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
        };

获取被点击标记的经纬度:

@Override
                public View getInfoWindow(Marker arg0) {
                    View v = getLayoutInflater().inflate(R.layout.infowindow,null);

                    Spot markerSpot = spots.get(arg0.getPosition().toString());
                        double Lat = markerSpot.getLatitude();
                        double Lon = markerSpot.getLongitude();
                        destLatitude = Lat;
                        destLongitude = Lon;
                        return v;
                    } else {
                        return null;
                    }
如果我尝试使用 sourceLatitude、sourceLongitude 获取它,

"LatLng start" 和 "LatLng end" 每次都是 0。 我希望任何人都可以帮助我解决问题,抱歉我的英语不好:)

您可以获得您当前的位置 LatLon:

....
    myMap.setMyLocationEnabled(true);
    LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    // Create a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Get the name of the best provider
    String provider = mlocManager.getBestProvider(criteria, true);

    // Get Current Location
    Location myLocation = mlocManager.getLastKnownLocation(provider);

    //get Lat Lon
    Double lat = myLocation.getLatitude();
    Double lon = myLocation.getLongitude();
....

我找到了解决问题的方法。 我在 onResponse 和 onClick 中设置了 LatLng 开始和 LatLng 结束,现在它可以工作了:)

gd = new GoogleDirection(this);
gd.setOnDirectionResponseListener(new OnDirectionResponseListener() {
public void onResponse(String status, Document doc, GoogleDirection gd) {
    LatLng start = new LatLng(sourceLatitude, sourceLongitude);
    LatLng end = new LatLng(destLatitude, destLongitude);
    mDoc = doc;
    myMap.addPolyline(gd.getPolyline(doc, 3, Color.RED));
    myMap.addMarker(new MarkerOptions().position(start)
    .icon(BitmapDescriptorFactory.defaultMarker(
    BitmapDescriptorFactory.HUE_GREEN)));
    myMap.addMarker(new MarkerOptions().position(end)
    .icon(BitmapDescriptorFactory.defaultMarker(
    BitmapDescriptorFactory.HUE_GREEN)));
    }
    });
buttonRequest = (Button)findViewById(R.id.routereq);
buttonRequest.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
    LatLng start = new LatLng(sourceLatitude, sourceLongitude);
    LatLng end = new LatLng(destLatitude, destLongitude);
       v.setVisibility(View.GONE);
       gd.setLogging(true);
       gd.request(start, end, GoogleDirection.MODE_DRIVING);
       }
       });