每次我在PlacePicker上更改位置时更新地图片段

时间:2018-05-27 11:15:59

标签: android google-maps fragment gmsplacepicker

我正在制作一个带有底部导航视图的应用程序,在我的主页上,我正在使用地图片段来显示我的位置,周围有2公里的蓝色圆圈。我想在我的活动中使用一个placepicker,这样每当我在placepicker上更改我的位置时,蓝色圆圈也应该移动到我到目前为止所做的相同位置。

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);
           } 
         }
       }
  }

片段内的代码

 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 个答案:

答案 0 :(得分:0)

当你将placePicker放到 -

时,你可以添加圈子
Year