【发布时间】:2015-08-19 13:11:21
【问题描述】:
首先,我只想说我不擅长解释(也不擅长编程),所以请耐心等待。
布局:
我有两个活动。 第一个是复选框和按钮所在的主要活动。 第二个活动是 google maps api。
进展:
当我点击按钮时,我进入了谷歌地图活动(使用意图)并有一个固定的位置。
问题:
如果在按下按钮时选中复选框,我想向谷歌地图发送位置搜索(例如:如果复选框显示保龄球,则搜索将是保龄球),但我没有不知道怎么写,也不知道在哪里写这段代码。
感谢任何链接/提示/答案:)
MainActivity
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);
}
});
}
地图活动
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,因为获取位置功能在模拟器上似乎不起作用。
【问题讨论】:
-
请发布您的代码。
-
你的问题太笼统了。这是解决您问题的教程:wptrafficanalyzer.in/blog/…
-
@RajanBhavsar 对不起,它说要尽量保持一般性:) 感谢教程,我会读一读:) 但是我不希望用户编写搜索,关键是让他们选择其中一个复选框:) 再次抱歉,感谢您的帮助,我会记住的!
标签: java android google-maps android-studio