【发布时间】: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 要求返回类型,但如果我更改它,则会告诉我该位置具有私有访问权限。
【问题讨论】: