0

我正在制作一个带有底部导航视图的应用程序,在我的主页上,我使用地图片段来显示我的位置,周围有一个 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));
 }
4

1 回答 1

0

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));
       } 
     }    
于 2018-05-27T12:04:35.790 回答