将LatLon从当前位置转换为标记

时间:2015-01-28 13:53:05

标签: android google-maps

我尝试将Polyline放在我的地图上,从当前位置到标记。 如果我将Lat und Longitude直接放在代码中,则Polyline正在绘制。 但是,如果我尝试从Locationlistener获取Lat和Lon,它不起作用,googlemaps链接如下所示: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) {
        }
        };

获取点击标记的Lat Lon:

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

&#34; LatLng start&#34;和&#34; LatLng结束&#34;如果我尝试使用sourceLatitude,sourceLongitude获取它,则每次都是0。 我希望有人能帮助我解决问题并抱歉我的英语不好:)

2 个答案:

答案 0 :(得分: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();
....

答案 1 :(得分:0)

我找到了解决问题的方法。 我在onResponse和onClick中设置LatLng start和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);
       }
       });