Update map fragment each time i change location on PlacePicker

144 views Asked by At

I am making an app with a bottom navigation view, on my home page i am using a map fragment to show my location with a blue circle of 2 km around it. i want to use a placepicker inside my activity so that whenever i change my location on placepicker the blue circle should also move to same location what i have done so far is this.

public class MainActivity extends AppCompatActivity {
LatLng myPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   Button btn = (Button) findViewById(R.id.changeLocation);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showPlacePicker();
        }
    });

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    OpeningPage2 fragment = new OpeningPage2();
    Bundle bundle = new Bundle();
    bundle.putString("KEY_LAT", String.valueOf(myPosition.latitude));
    bundle.putString("KEY_LNG", String.valueOf(myPosition.longitude));
    fragment.setArguments(bundle);
    ft.replace(R.id.content,fragment);
    ft.commit(); 
    }

    private void showPlacePicker() {
    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
    try {
        startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
    } catch (Exception e) {
        Log.e(TAG, e.getStackTrace().toString());
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_PICKER_REQUEST) {
        if (resultCode == RESULT_OK) {
            Place place = PlacePicker.getPlace(data, 
            getApplicationContext());
            StringBuilder stBuilder = new StringBuilder();

            Double latitude1 = Double.valueOf(place.getLatLng().latitude);
            Double longitude1 = Double.valueOf(place.getLatLng().longitude);
            myPosition = new LatLng(latitude1, longitude1);
           } 
         }
       }
  }

Code Inside Fragment

 double lat,lng;
 @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        String latitude, longitude;
        latitude = getArguments().getString("KEY_LAT");
        longitude = getArguments().getString("KEY_LNG");
        lat = Double.valueOf(latitude);
        lng= Double.valueOf(longitude);
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
 }

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


    SupportMapFragment mapFragment = (SupportMapFragment) 
  getChildFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

  }
  public void onMapReady(final GoogleMap googleMap) {
  mMap = googleMap;
    mUiSettings = mMap.getUiSettings();
    mUiSettings.setZoomControlsEnabled(true);
    mUiSettings.setCompassEnabled(true);
    mUiSettings.setMyLocationButtonEnabled(true);
 myPosition = new LatLng(lat, lng);
 mMap.addCircle(new CircleOptions()
                                .center(myPosition)
                                .radius(2000)
                                .strokeColor(Color.BLACK)
                                .fillColor(0x220000FF)
                                .strokeWidth(1)
                        );
 mMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));
 }
1

There are 1 answers

7
Raj On

You can add circle when you get pace fro placePicker as -

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
    if (resultCode == RESULT_OK) {
        Place place = PlacePicker.getPlace(data, 
        getApplicationContext());
        StringBuilder stBuilder = new StringBuilder();

        Double latitude1 = Double.valueOf(place.getLatLng().latitude);
        Double longitude1 = Double.valueOf(place.getLatLng().longitude);
        myPosition = new LatLng(latitude1, longitude1);

        mMap.clear();

        mMap.addCircle(new CircleOptions()
                                .center(myPosition)
                                .radius(2000)
                                .strokeColor(Color.BLACK)
                                .fillColor(0x220000FF)
                                .strokeWidth(1)
                        );
 mMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));
       } 
     }