0

Actually i want to implement a google map where i can find my current location with blue marker and circulating this marker i want nearby hospitals by max interval 1KM with red markers and when i click on any marker i want to display the route to it,Any ideas?

Here is my code of my current location

   package com.example.stcnhg.enkaz;
    import android.Manifest;
    import android.content.pm.PackageManager;
    import android.location.Address;
    import android.location.Geocoder;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.support.annotation.Nullable;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;

    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;

    import java.io.IOException;
    import java.util.List;

    import static android.content.Context.LOCATION_SERVICE;

    public class MapsActivity extends Fragment implements OnMapReadyCallback {

        private GoogleMap mMap;
        LocationManager locationManager;

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.activity_maps, container, false);
            SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
            locationManager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);
            if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                         int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.

            }
            if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){

                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 20, new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        double latitude = location.getLatitude();
                        double longitude = location.getLongitude();
                        LatLng latLng = new LatLng(latitude,longitude);
                        Geocoder geocoder = new Geocoder(getContext());
                        try {
                            List<Address> addressList = geocoder.getFromLocation(latitude,longitude,1);
                            String str = addressList.get(0).getLocality()+",";
                            str += addressList.get(0).getCountryName();
                            mMap.addMarker(new MarkerOptions().position(latLng).title(str));
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,12));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {

                    }

                    @Override
                    public void onProviderEnabled(String provider) {

                    }

                    @Override
                    public void onProviderDisabled(String provider) {

                    }
                });
            }

            return v;
        }



        /**
         * Manipulates the map once available.
         * This callback is triggered when the map is ready to be used.
         * This is where we can add markers or lines, add listeners or move the camera. In this case,
         * we just add a marker near Sydney, Australia.
         * If Google Play services is not installed on the device, the user will be prompted to install
         * it inside the SupportMapFragment. This method will only be triggered once the user has
         * installed Google Play services and returned to the app.
         */
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;

            // Add a marker in Sydney and move the camera
           // LatLng sydney = new LatLng(-34, 151);
            //mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
            //mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
            //mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney,12.0f));
        }

    }
Omar
  • 1
  • 5
  • 1
    Write your logic in @Override public void onLocationChanged(Location location) { } This is the method of LocationListener Interface. – lalit jadhav Jun 16 '17 at 06:12
  • @lalitjadhav and what is the logic? i want a tutorial to satisfy this requirements – Omar Jun 16 '17 at 06:14
  • ok will post my answer soon. – lalit jadhav Jun 16 '17 at 06:15
  • @lalitjadhav Thank you, That would be great. – Omar Jun 16 '17 at 06:16
  • 1
    Use google places api which will give you all the nearby place. By using this you dont need to write logic to find hospitals nearby https://developers.google.com/places/android-api/start – lalit jadhav Jun 16 '17 at 06:18
  • you can use this link. It is similar to your question https://stackoverflow.com/questions/8428209/show-current-location-and-nearby-places-and-route-between-two-places-using-googl – RajatN Sep 04 '17 at 11:31

1 Answers1

2

Use google places api which will give you all the nearby place. By using this you dont need to write logic to find hospitals nearby developers.google.com/places/android-api/start

lalit jadhav
  • 320
  • 1
  • 4
  • 13