简体   繁体   English

打开我的应用程序时,Android Studio Google Maps如何将相机重定向到我的当前位置

[英]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.

相关问题 如何在谷歌地图中重定向我当前的位置 - How to Redirect my current Location in google Maps 如何从当前应用程序打开** Google Maps **应用程序? - How to open **Google Maps** application from my current application? 如何使用谷歌地图 API 在 android 工作室找到我当前的位置? - How to find my current location in android studio using Google Maps API? 如何在Android应用程序地图中直接缩放到当前位置 - how i can zoom directly to my current location in android application maps 如何在android地图中获取我当前的位置? - How to get my current location in android maps? 如何从我的应用程序打开标准的谷歌地图应用程序,然后选择一个位置然后重定向回我的应用程序? - how to open standard Google Map application from my application and after choose a location then redirect back to my application? 打开我的应用程序时如何将相机移动到当前位置? - How to move the camera on current position when I open my app? android 谷歌地图将相机动画缓慢地移动到我的位置 - android google maps animate the camera slowly to my location 如何在Android的Google地图上显示我的位置 - How can I show my location on google maps in android moveCamera无法更新到我在Google Maps Android上的当前位置吗? - moveCamera not updating to my current location on Google Maps Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM