Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps Api V2 draw circle around current position

Im currently working with Google Maps API V2 on an android project, I need to be able to draw a circle around the users current position and display markers for points of interest within the radious around the users current position.

Im struggling to get the small marker that denotes the users current position so i can draw a circle around it.

This is my code so far.

//Set up map and display users current position.
    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.nearbyMapSearch);
    mapFragment.getMapAsync(this);

    final GoogleMap map = (((MapFragment) getFragmentManager().findFragmentById(R.id.nearbyMapSearch)).getMap());
    map.setMyLocationEnabled(true);

    //zoom to users location
    GeoLocationHandler geoHandler = new GeoLocationHandler(getActivity().getApplicationContext());
    Log.i("Search Fragment", "" + geoHandler.getCurrentLatitude() + " , " + geoHandler.getCurrentLongitude());
    LatLng loc = new LatLng(geoHandler.getCurrentLatitude(), geoHandler.getCurrentLongitude());

    //Begin animating camera.
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(
            new LatLng(loc.latitude, loc.longitude), 13));

    //Instantiate new Camera position
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(loc.latitude, loc.longitude))
            .zoom(3)
            .bearing(0)
            .tilt(0)
            .build();
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

Any help with this would be much appreciated thanks.

like image 988
Marc Davies Avatar asked Dec 12 '25 23:12

Marc Davies


1 Answers

Circle circle = map.addCircle(new CircleOptions()
     .center(new LatLng(loc.latitude, loc.longitude))
     .radius(1000)
     .strokeColor(Color.RED)
     .fillColor(Color.BLUE));

More info https://developers.google.com/android/reference/com/google/android/gms/maps/model/Circle

You should subscribe to updates of user location and reset the circle center with

circle.setCenter(newLocation);
like image 88
Gabriel Avatar answered Dec 16 '25 06:12

Gabriel