簡體   English   中英

打開我的應用程序時,Android Studio Google Maps如何將相機重定向到我的當前位置

[英]Android Studio Google Maps how to redirect camera to my current location when i open my application

打開我的應用程序時,相機動畫無法正常工作,無法定向到我的位置,並且它也未添加標記

 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();
}

在這里它不會將我的相機對准我的當前位置。 當我打開應用程序時,它開始於地圖中心而不是我的位置

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"));
    }
}
}

您有一些巨大的超級基本錯誤:

僅當baground中的活動進入前台時才調用first onResume setUpMapIfNeeded()放入onCreate中。

在最后一個if語句中,函數setUpMapIfNeeded()中的第二個函數不是放置setUpMap()而是放置setUpMapIfNeeded()改變它,它應該可以工作。

public class MapsActivity extends  FragmentActivity {

private GoogleMap mMap; // Might be null if Google Play services APK is not available.

這是修改后的代碼:

第1部分

@Override
 protected void onCreate(Bundle 
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();

}


@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}

第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));
            }
        });
}

只需更改這兩種方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM