【问题标题】:addMarker and moveCamera invalid declarationsaddMarker 和 moveCamera 无效声明
【发布时间】:2020-03-01 09:12:19
【问题描述】:

我正在实现一个地图,它需要在运行时显示您的当前位置。到目前为止,我没有遇到任何问题,但是今天在学习如何设置 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));
            }
        };
    }



}

这是我们遇到问题的片段:

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

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

【问题讨论】:

    标签: java android


    【解决方案1】:

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

    您已将代码放置在通常有 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 中。我解决了这个问题。感谢您的帮助!
    猜你喜欢
    • 2014-07-20
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    • 2020-01-25
    • 2021-08-11
    相关资源
    最近更新 更多