gpt4 book ai didi

java - 将搜索参数从 Activity 发送到 google map api (Android studio)

转载 作者:行者123 更新时间:2023-12-01 11:13:39 24 4
gpt4 key购买 nike

首先,我想说的是,我不擅长解释(也不擅长编程),所以请耐心等待。

布局:

我有两项 Activity 。第一个是主要 Activity ,其中有一个复选框和一个按钮。第二个 Activity 是 Google map API。

进展:

当我点击按钮时,我进入谷歌地图 Activity (使用 Intent )并有一个固定的位置。

问题:

如果按下按钮时选中复选框,我想向谷歌地图发送位置搜索(例如:如果复选框显示保龄球,则搜索将是保龄球),但我不'不知道如何或在哪里编写此代码。

任何链接/提示/答案表示赞赏:)

主要 Activity

private static Button button_Ok;
private static CheckBox bowling;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onClickButtonListener();
}

public void onClickButtonListener() {
button_Ok = (Button)findViewById(R.id.button);
bowling = (CheckBox)findViewById(R.id.checkBox);
button_Ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.google.android.gms.maps.SupportMapFragment");
startActivity(intent);
}
});
}

map Activity

private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
mMap.setMyLocationEnabled(true);

// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();

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

// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);

// set map type
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

// Get latitude of the current location
double latitude = *fixed*; //myLocation.getLatitude();

// Get longitude of the current location
double longitude = *fixed*; //myLocation.getLongitude();

// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);

// Show the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located"));

LatLng myCoordinates = new LatLng(latitude, longitude);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(myCoordinates, 12);
mMap.animateCamera(yourLocation);

CameraPosition cameraPosition = new CameraPosition.Builder()
.target(myCoordinates) // Sets the center of the map to LatLng (refer to previous snippet)
.zoom(13) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(0) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

// ... get a map.
Circle circle = mMap.addCircle(new CircleOptions()
.center(new LatLng(*fixed*, *fixed*))
.radius(100)
.strokeColor(Color.RED)
.fillColor(Color.TRANSPARENT));


mMap.getUiSettings().setZoomControlsEnabled(true);
}

}

我设置了固定的 LatLng,因为获取位置功能似乎在模拟器上不起作用。

最佳答案

我不确定您将使用的代码,因为有大约十几种不同的方法可以做到这一点,但我可以建议如何实现此功能。

我将有一个 Activity MapActivity 和两个 fragment 。第一个 fragment 包含复选框和搜索按钮 (buttonFragment),第二个 fragment 将显示您的 Google map (mapFragment)。通过这种方式,您只需初始化和设置 GoogleMaps 一次,就可以从 ButtonFragment 访问 API 的功能。

按下按钮时,您将使用 GoogleMaps 获取您的位置,使用 Google Places API 您可以返回附近地点的列表,然后您可以使用接口(interface)回调将此信息传递给您的 Activity,然后该 Activity 将启动 mapFragment并传递从buttonFragment接收到的信息。

Google map API: https://developers.google.com/maps/

Google 地方信息 API: https://developers.google.com/places/

关于java - 将搜索参数从 Activity 发送到 google map api (Android studio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32096488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com