Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java addMarker和moveCamera声明无效_Java_Android - Fatal编程技术网

Java addMarker和moveCamera声明无效

Java addMarker和moveCamera声明无效,java,android,Java,Android,我正在实现一个地图,需要在运行时显示您的当前位置。到目前为止,我还没有遇到任何问题,但今天,在学习如何设置Lat和Lng的教程后,我在为我的项目初始化地图标记时遇到了麻烦。无法解析addMarker和moveCamera。我使用的是API 29,可能这就是问题所在,因为我正试图强制Android Studio使用v4片段活动。我的XML文件没问题,这是我的代码。我认为我的部分旧代码与我下面的新教程相冲突。我请求你的支持 import android.Manifest; import androi

我正在实现一个地图,需要在运行时显示您的当前位置。到目前为止,我还没有遇到任何问题,但今天,在学习如何设置Lat和Lng的教程后,我在为我的项目初始化地图标记时遇到了麻烦。无法解析addMarker和moveCamera。我使用的是API 29,可能这就是问题所在,因为我正试图强制Android Studio使用v4片段活动。我的XML文件没问题,这是我的代码。我认为我的部分旧代码与我下面的新教程相冲突。我请求你的支持

import android.Manifest;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import android.content.Context;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.SearchView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.location.LocationListener;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;

import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.material.bottomnavigation.BottomNavigationView;`


import java.io.IOException;
import java.util.List;

public class About extends FragmentActivity implements OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {



    LocationManager locationManager;
    LocationListener locationListener;
    LatLng userLatLong;
    GoogleMap map;
    SupportMapFragment mapFragment;
    SearchView searchView;

    LatLng test= new LatLng(57, 2);
    map.addMarker(new MarkerOptions().position(test).title("Marker Test"));
    map.moveCamera(CameraUpdateFactory.newLatLng(test));


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


        searchView = findViewById(R.id.sv_location);
        mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.google_map);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                String location = searchView.getQuery().toString();
                List<Address> addressList = null;

                if (location != null || !location.equals("")) {
                    Geocoder geocoder = new Geocoder(About.this);
                    try {
                        addressList = geocoder.getFromLocationName(location, 1);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Address address = addressList.get(0);
                    LatLng latLng = new LatLng(address.getLatitude(),address.getLongitude());
                    map.addMarker(new MarkerOptions().position(latLng).title(location));
                    map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });



        mapFragment.getMapAsync(this);

    private void fetchLastLocation() {
      //  Task<Location> task = fusedLocationProviderClient.getLastLocation();
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                // store user latLong
                userLatLong = new LatLng(location.getLatitude(), location.getLongitude());
                map.clear(); //clear old location marker
                map.addMarker(new MarkerOptions().position(aberdeen).title("Marker in Aberdeen"));
                map.moveCamera(CameraUpdateFactory.newLatLng(aberdeen));
            }
        };
    }



}

这是说MarkerOptions要求返回类型,但如果我更改它,它会告诉我position具有私人访问权限。

不确定这是发布错误还是您的实际代码,但它不正确

您已经将代码放置在通常存在memberVariableDeclarations的位置。这就是编译器所抱怨的

如果这只是文章中的一个错误-那么请更正文章,我将删除此

public class About extends FragmentActivity implements OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {



    LocationManager locationManager;
    LocationListener locationListener;
    LatLng userLatLong;
    GoogleMap map;
    SupportMapFragment mapFragment;
    SearchView searchView;

    //-------------------------------------------------------------------//
    //*** This section is misplaced ***//

    LatLng test= new LatLng(57, 2);  // this part can stay if needed but
                                     // next 2 lines definitely need to go.
    map.addMarker(new MarkerOptions().position(test).title("Marker Test"));
    map.moveCamera(CameraUpdateFactory.newLatLng(test));
    //-------------------------------------------------------------------//

对我将代码放在memberVariableDeclarations中。我解决了这个问题。谢谢你的帮助!
public class About extends FragmentActivity implements OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {



    LocationManager locationManager;
    LocationListener locationListener;
    LatLng userLatLong;
    GoogleMap map;
    SupportMapFragment mapFragment;
    SearchView searchView;

    //-------------------------------------------------------------------//
    //*** This section is misplaced ***//

    LatLng test= new LatLng(57, 2);  // this part can stay if needed but
                                     // next 2 lines definitely need to go.
    map.addMarker(new MarkerOptions().position(test).title("Marker Test"));
    map.moveCamera(CameraUpdateFactory.newLatLng(test));
    //-------------------------------------------------------------------//