[英]Sending search parameter from activity to google maps api (Android studio)
首先,请说我不是最擅长解释(也不是编程),所以请耐心等待。
布局:
我有两个活动。 第一个是复选框和按钮所在的主要活动。 第二个活动是Google Maps API。
进度:
当我单击按钮时,我进入google maps活动(使用意图)并具有固定位置。
问题:
如果要在按下按钮时选中此复选框,我想向Google地图发送位置搜索( 例如:如果该复选框显示“保龄球”,则搜索将用于“保龄球” ),但是我不知道如何在哪里编写此代码。
任何链接/提示/答案表示赞赏:)
主要活动
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);
}
});
}
MapsActivity
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,因为get位置功能似乎在模拟器上不起作用。
我不确定您将使用的代码,因为有大约十二种不同的方法可以执行此操作,但是我可以建议如何实现此功能。
我将有一个活动MapActivity
和两个片段。 一个包含复选框和搜索按钮的片段( buttonFragment
),第二个片段将显示您的Google Maps( mapFragment
)。 这样,您只需初始化和设置GoogleMaps一次,就可以从buttonFragment访问API的功能。
按下按钮时,您将使用GoogleMaps获取位置,使用Google Places API可以返回附近地点的列表,然后使用接口回调将此信息传递给您的活动,然后该接口回调将启动mapFragment并传递从buttonFragment收到的信息。
Google Maps API: https : //developers.google.com/maps/
Google Places API: https : //developers.google.com/places/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.