[英]Android Studio Google Maps how to redirect camera to my current location when i open my application
The Camera Animation is not working its not directing to my location when I open my Application and it does not add a Marker either 打开我的应用程序时,相机动画无法正常工作,无法定向到我的位置,并且它也未添加标记
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
Here it wont direct my camera in my current location. 在这里它不会将我的相机对准我的当前位置。 When I open my application it starts at the center of the map not my location
当我打开应用程序时,它开始于地图中心而不是我的位置
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMapIfNeeded();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet"));
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
LatLng target = new LatLng(location.getLatitude(), location.getLongitude());
CameraPosition position = this.mMap.getCameraPosition();
CameraPosition.Builder builder = new CameraPosition.Builder();
builder.zoom(15);
builder.target(target);
this.mMap.animateCamera(CameraUpdateFactory.newCameraPosition(builder.build()));
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("You are here!").snippet("Consider yourself located"));
}
}
}
You have few huge super basic errors: 您有一些巨大的超级基本错误:
First onResume
is only called when activity in the baground comes to the foreground. 仅当baground中的活动进入前台时才调用first
onResume
。 Put setUpMapIfNeeded()
Inside onCreate
as well. 将
setUpMapIfNeeded()
放入onCreate
中。
Second in your function setUpMapIfNeeded()
in the last if
statement instead of putting setUpMap()
you put setUpMapIfNeeded()
change that and it should work. 在最后一个
if
语句中,函数setUpMapIfNeeded()
中的第二个函数不是放置setUpMap()
而是放置setUpMapIfNeeded()
改变它,它应该可以工作。
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
Here is your code modified:
这是修改后的代码:
Part 1
第1部分
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
Part 2
第2部分
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new.MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet"));
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
LatLng target = new LatLng(location.getLatitude(), location.getLongitude());
CameraPosition position = this.mMap.getCameraPosition();
CameraPosition.Builder builder = new CameraPosition.Builder();
builder.zoom(15);
builder.target(target);
this.mMap.animateCamera(CameraUpdateFactory.newCameraPosition(builder.build()));
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("You are here!").snippet("Consider yourself located"));
}
}
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng l = new LatLng(location.getLatitude(), location.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(l, mMap.getCameraPosition().zoom));
}
});
}
Just change this 2 methods 只需更改这两种方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.