6

Hello stackoferflow users.

I am developing android App and this app implement Google Play Service

I already get my location and set a pin and also a circle on my maps.

What i want to achieve is whenever i move to someplace, the circle will also move and put my position as the center location.

My question is :

  1. How to update my current location every 2-5 second and the circle will also move to my new current location

  2. How to set my circle area as an area where the marker will place, so if the marker not in the area of my circle, it will not shown on the map.

Thank you

This is my code that i use for maps:

    mMap.setMyLocationEnabled(true);
    mMap.getUiSettings().setCompassEnabled(true);
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
    mMap.getUiSettings().setRotateGesturesEnabled(true);

    locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    // Creating a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Getting the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);

    // Getting Current Location
    Location location = locationManager.getLastKnownLocation(provider);

    mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {
            View v = getLayoutInflater().inflate(R.layout.marker, null);
            TextView title= (TextView) v.findViewById(R.id.title);
            TextView info= (TextView) v.findViewById(R.id.info);
            title.setText(marker.getTitle().toString());
            info.setText(marker.getSnippet().toString());
            return v;
        }
    });

    if(location != null){
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        myPosition = new LatLng(latitude, longitude);   

        CameraUpdate center = CameraUpdateFactory.newLatLngZoom(myPosition, 15);
        mMap.moveCamera(center);
        mMap.addMarker(new MarkerOptions()
            .position(myPosition)
            .alpha(0.8f)
            .anchor(0.0f, 1.0f)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.blue_pin))
            .title("Your position :\n ")
            .snippet(latitude + " and " + longitude));

        CircleOptions circleOptions = new CircleOptions()
          .center(myPosition)   //set center
          .radius(rad)   //set radius in meters
          .fillColor(0x402092fd)  //default
          .strokeColor(Color.LTGRAY)
          .strokeWidth(5);
          circle = mMap.addCircle(circleOptions);   

          CameraPosition cameraPosition = CameraPosition.builder()
                  .target(myPosition)
                  .zoom(15)
                  .bearing(90)
                  .build();

         mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition),2000, null);
    }

Thank you =)

Christ Samuel
  • 109
  • 2
  • 4
  • 10
  • http://stackoverflow.com/questions/12451722/location-updates-every-35-seconds-and-draw-a-circle-on-the-current-location?rq=1[check here][1] [1]: http://stackoverflow.com/questions/12451722/location-updates-every-35-seconds-and-draw-a-circle-on-the-current-location?rq=1 – ask4solutions Jan 28 '14 at 10:52
  • Do you have another example @ask4solutions ? I don't know how to implement it with my code. I mean i try it but it doesn't seems to work and also im not using mapView. – Christ Samuel Jan 29 '14 at 03:28

3 Answers3

3

Hello for updating your location every second you have to use broadcasting.using this your service start so can put your map code in thread where you put delay 1 second so you get location every second.you also put this method in broadcast receiver class.

    public void onReceive(final Context context, Intent intent) {
        this.context = context;
        Log.i(TAG, "onReceive");

        locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            listener = new LocationListener() {

                @Override
                public void onLocationChanged(Location location) {
                    // double valueLatitude = location.getLatitude();
                    // double valueLongitude= location.getLongitude();
                    double precision = Math.pow(10, 6);
                    double valueLatitude = ((int) (precision * location
                            .getLatitude())) / precision;
                    double valueLongitude = ((int) (precision * location
                            .getLongitude())) / precision;
                    Log.i(TAG, "onLocationChanged");
                    Log.v(TAG, "LAT: " + valueLatitude + " & LONG: "
                            + valueLongitude);
                    String lat = String.valueOf(valueLatitude);
                    String lng = String.valueOf(valueLongitude);
                    SessionManager.saveLocation(valueLatitude, valueLongitude, context);
                    Log.v(TAG, "LAT: SESSION" + SessionManager.getlattitude(context));
                    try {
                        if (!SessionManager.getlattitude(context).equals(
                                valueLatitude)
                                || !SessionManager.getlongitude(context)
                                .equals(valueLongitude)) {

                            SessionManager.saveLocation(valueLatitude,
                                    valueLongitude, context);
//                            if (Utils.progrsDia.isShowing()) {
//                                Utils.progrsDia.dismiss();
//                            }
//                            CategoryNearbyFragment.callGetNearByFlyerListListner
//                                    .callGetNearByFlyer(lat, lng);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onProviderDisabled(String arg0) {
                }

                @Override
                public void onProviderEnabled(String arg0) {
                }

                @Override
                public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                }

            };
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, 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.
                return;
            }
            locationManager.requestSingleUpdate(
                    LocationManager.NETWORK_PROVIDER, listener, null);
        } else {
            // GlobalData.showSettingsAlert(context);
        }
    }
Sachin Suthar
  • 692
  • 10
  • 28
0

For your first question How to update my current location every 2-5 second and the circle will also move to my new current location

I suggest you take a look at the excellent and complete sample at Googles Receiving Location Updates guide.

It includes sample code and an Android project you can import in ADT straight away.

The callback method that Location Services invokes to send a location update to your app is specified in the LocationListener interface, in the method onLocationChanged().

There you can put your camera update.

For your query How to set my circle area as an area where the marker will place follow this link

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • I try to implement it @shylendra. But still it doesn't work for the first question. The location is updated but the circle still get my last latlng location not the new one. Do you have another example ? For the second question is, i want the marker only show up on circle radius. Do you know how to do this ?? – Christ Samuel Jan 29 '14 at 03:56
0

I'll suggest using the onLocationChange() method to load refreshed coords into the lat long variables and then use a handler timed for like 5 seconds or 10seconds to update the map object using the moveCamera() functions ,this way you will get steady updates that are not too frequently annoying!

Naaz
  • 273
  • 2
  • 6
  • 21