Google Maps v2 - set both my location and zoom in
可能重复protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_g_maps);
GoogleMap googleMap;
LatLng myPosition;
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
myPosition = new LatLng(latitude, longitude);
}
}
}
我尝试添加:
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(latitude,
longitude));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
googleMap.moveCamera(center);
googleMap.animateCamera(zoom);
或
googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude) ,4) );
但是一旦我点击调用此方法的按钮,Google地图就不会自动放大。我仍然可以使用Google界面来点击我的位置,但我想让它自动放大。
请帮忙吗?
答案 0 :(得分:13)
尝试这是您的问题的简单解决方案
LatLng coordinate = new LatLng(lat, lng);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
map.animateCamera(yourLocation);
还有..
可以一次改变位置,变焦,方位和倾斜。也可以在animatecamera调用上设置持续时间。
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MOUNTAIN_VIEW) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
在这里查看文档:
https://developers.google.com/maps/documentation/android/views?hl=fr-FR#moving_the_camera
答案 1 :(得分:4)
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
Location locationCt;
LocationManager locationManagerCt = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationCt = locationManagerCt
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng latLng = new LatLng(locationCt.getLatitude(),
locationCt.getLongitude());
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.addMarker(new MarkerOptions().position(latLng)
.title("My Spot").snippet("This is my spot!")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_car)));
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
答案 2 :(得分:1)
在您的XML文件中
<Button
android:id="@+id/Bzoomin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onZoom"
android:text="^" />
<Button
android:id="@+id/Bzoomout"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="onZoom"
android:text="v" />'
在您的Java文件中
public void onZoom(View view)
{
if(view.getId() == R.id.Bzoomin)
{
mMap.animateCamera(CameraUpdateFactory.zoomIn());
}
if(view.getId() == R.id.Bzoomout)
{
mMap.animateCamera(CameraUpdateFactory.zoomOut());
}
}
答案 3 :(得分:0)
对于Kotlin
val location = CameraUpdateFactory.newLatLngZoom(latlng, 15F)
mMap.animateCamera(location)