【问题标题】:Get LatLon from current Position to Marker从当前位置获取 LatLon 到标记
【发布时间】:2015-03-27 10:54:51
【问题描述】:

我尝试在我的地图上从当前位置到标记放置一条折线。 如果我将纬度和经度直接放在代码中,则折线正在绘制。 但是,如果我尝试从 Locationlistener 中获取纬度和经度,它不起作用,并且 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) {
        }
        };

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

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

“LatLng 开始”和“LatLng 结束”如果我尝试使用 sourceLatitude、sourceLongitude 获取它,则每次都为 0。 我希望任何人都可以帮助我解决问题,并对我的英语不好表示抱歉:)

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    你可以得到你当前的位置 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();
    ....
    

    【讨论】:

    • 您好,感谢您的快速回复。对不起,我忘了把它粘贴到上面的代码中。现在我的代码更新了
    【解决方案2】:

    我找到了解决问题的方法。 我在 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);
           }
           });
    

    【讨论】:

      猜你喜欢
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多