我在主要活动中有onPlaceSelected(放置位置)方法我想在地图片段中使用它,在所选位置设置标记位置。地图片段是主要活动中的片段(在导航抽屉中),我该怎么做 这是我的代码..
@Override
public void onPlaceSelected(Place place) {
Log.i(TAG, "Place Selected: " + place.getName());
// Format the returned place's details and display them in the TextView.
mPlaceDetailsText.setText(formatPlaceDetails(getResources(), place.getName(), place.getId(),
place.getAddress(), place.getPhoneNumber(), place.getWebsiteUri(),place.getLatLng()));
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
final LatLng location = place.getLatLng();
AppConstants.SHOW_SEARCH_LOCATION=address;
AppConstants.SHOW_SEARCH_LATLNG=location;
Log.d("placedetails",name+","+address+" "+location);
CharSequence attributions = place.getAttributions();
if (!TextUtils.isEmpty(attributions)) {
mPlaceAttribution.setText(Html.fromHtml(attributions.toString()));
} else {
mPlaceAttribution.setText("");
}
}
答案 0 :(得分:3)
你只需要添加
mMap.addMarker(new MarkerOptions().position(place.getLatLng()));
到您的onPlaceSelected
方法。
mMap
是一个你可以这样得到的地图实例:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
this.mMap = map;
}
}
答案 1 :(得分:0)
按下placeSelectedListener
中的autoCompleteFragment
之后,您需要回叫加载的地图。
位置:在AutocompleteSupportFragment
上搜索所需的位置,然后按所需的特定位置后,相机将缩放到该位置并标记该位置。
方法setAutoCompleteFragment
private void setAutocompleteFragment() {
// Specify the types of place data to return.
assert autocompleteFragment != null;
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG));
// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(@NonNull final Place place) {
if (place.getLatLng() != null) {
buttonSubmit.setVisibility(View.VISIBLE);
latitude = place.getLatLng().latitude;
longitude = place.getLatLng().longitude;
name = place.getName();
// Creating a marker
final MarkerOptions markerOptions = new MarkerOptions();
Toast.makeText(getApplicationContext(), String.valueOf(place.getLatLng()), Toast.LENGTH_LONG).show();
// Setting the position for the marker
markerOptions.position(place.getLatLng());
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(name);
// Clears the previously touched position
mMap.clear();
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
// Animating to the touched position
mMap.moveCamera(CameraUpdateFactory.newLatLng(place.getLatLng()));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 13.3f));
// Placing a marker on the touched position
mMap.addMarker(markerOptions);
}
});
}
}
@Override
public void onError(@NonNull Status status) {
Toast.makeText(getApplicationContext(), "Error: " + status, Toast.LENGTH_LONG).show();
}
});
}
然后我输入了onCreate。这样。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_package_location);
view = findViewById(R.id.view_auto_complete_support_fragment);
buttonSubmit = findViewById(R.id.button_submit);
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
// Initialize the AutocompleteSupportFragment.
autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
setViewOnCancel();
setButtonSubmit();
mapFragment.onCreate(savedInstanceState);
mapFragment.getMapAsync(this);
// Initialize the SDK
Places.initialize(getApplicationContext(), getResources().getString(R.string.google_api_key));
// Create a new Places client instance
Places.createClient(this);
setAutocompleteFragment();
}