简体   繁体   中英

Adding markers to a map in Android

I have a KML layer which I form from an input Stream as so:

private KmlLayer layer;
inputStream = new URL("urlHERE").openStream();
layer = new KmlLayer(mMap, inputStream, getApplicationContext());

I want to iterate through each marker in this layer and check if it is within a certain distance to my current location and display it only if it is.

This is the code I am using to currently get the lat/long of the markers.

for (KmlPlacemark placemark: layer.getPlacemarks()) {


            String s = placemark.getGeometry().getGeometryObject().toString();
            Log.d("placemarks",s);
            String start = "(";
            String end = ")";
            String latlngvalue = s.substring(s.indexOf(start)+1,s.lastIndexOf(end));
            String[] strngs = latlngvalue.split(",");
            double placemarkerLat = Double.parseDouble(strngs[0]);
            double placemarkerLong = Double.parseDouble(strngs[2]);
            markerLocation.setLatitude(placemarkerLat);
            markerLocation.setLongitude(placemarkerLong);

What I want to do is add the information for each Marker in an ArrayList if they are within a certain distance of the user and then add the markers onto the map.

CHECK THIS LINE

currLocationMarker = mGoogleMap.addMarker(markerOptions); //YOUR ANSWER HERE

 @Override
 public void onLocationChanged(Location location) {

    mGoogleMap.clear();
    mHashMap.clear();

    allMarkersLists.clear();
    favouriteGpList.clear();

    //place marker at current position
    //mGoogleMap.clear();
    if (currLocationMarker != null) {
        currLocationMarker.remove();
    }

    latitude = location.getLatitude();
    longitude = location.getLongitude();
    latLng = new LatLng(location.getLatitude(), location.getLongitude());

    markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).draggable(true);
    currLocationMarker = mGoogleMap.addMarker(markerOptions); //YOUR ANSWER HERE

    mHashMap.put(currLocationMarker, 0);

    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11));
    mGoogleMap.setMyLocationEnabled(true);

    getMapPin();

    //If you only need one location, unregister the listener
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM