addMarker和moveCamera

addMarker和moveCamera

我正在实施一张地图,该地图需要在运行时显示您的当前位置。到目前为止,我还没有遇到任何问题,但是今天,在遵循有关如何设置纬度和经度的教程时,我很难为项目初始化地图标记。无法解析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));
            }
        };
    }



}


这是我们遇到的片段:

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


据说MarkerOptions正在要求返回类型,但是如果我更改它,则会告诉我该职位具有私人访问权限。

最佳答案

不知道这是发帖错误还是您的实际代码,但不正确。

您已将代码放置在通常存在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));
    //-------------------------------------------------------------------//

07-27 16:05