gpt4 book ai didi

java - addMarker 和 moveCamera 声明无效

转载 作者:行者123 更新时间:2023-12-02 09:00:36 27 4
gpt4 key购买 nike

我正在实现一个 map ,需要在运行时显示您当前的位置。到目前为止,我没有遇到任何问题,但今天在学习如何设置纬度和经度的教程时,我在为我的项目初​​始化 map 标记时遇到了麻烦。 addMarker 和 moveCamera 无法解析。我正在使用 API 29,也许这就是问题所在,因为我试图强制 Android Studio 使用 v4 fragment Activity 。我的 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));
}
};
}



}

这是我们遇到问题的 fragment :

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

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

最佳答案

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

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

关于java - addMarker 和 moveCamera 声明无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60189652/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com