java - 在地图中获取用户位置
问题描述
我正在尝试在地图中获取用户的位置。我有两个页面——一个页面有一个“点击显示我的位置”按钮,另一个页面在点击按钮时会指向地图。打开应用程序时,我只想要第二页。但我不知道如何更正代码。我在这里检查了其他一些问题,但我无法解决我的问题。请帮助,因为我是初学者。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showMyLocation(View view) {
startActivity(new Intent(getApplicationContext(),MapsActivity.class));
}
}
MapsActivity.java
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
private GoogleMap mMap;
LocationManager locationManager;
private static final int REQUEST_LOCATION_PERMISSION = 1;
Marker marker;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION_PERMISSION);
}
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
//get the location name from latitude and longitude
Geocoder geocoder = new Geocoder(getApplicationContext());
try {
List<Address> addresses =
geocoder.getFromLocation(latitude, longitude, 1);
String result = addresses.get(0).getLocality()+":";
result += addresses.get(0).getCountryName();
LatLng latLng = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(latLng).title(result));
mMap.setMaxZoomPreference(20);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 12.0f));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
// LatLng sydney = new LatLng(-34, 151);
// mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
解决方案
您没有showMyLocation
在 onCreate 中调用该方法。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showMyLocation();
}
//Removed the view object paramter as its not required
public void showMyLocation() {
startActivity(new Intent(getApplicationContext(),MapsActivity.class));
}
}
MainActivity
最佳做法是,如果您与then 设置MapsActivity
为启动器活动没有任何关系。
推荐阅读
- jquery - ASP.NET MVC 无法让 Jquery 验证在 _layout 中的表单上工作
- c# - 为什么 MassTransit 中的简单配置会创建 2 个队列和 3 个交换?
- xamarin - 如何摆脱 Xamarin Forms 中网格行之间的空白行
- swift - 多线程核心数据对象在后台线程上保存后在主线程上具有空属性
- azure - 尝试使 AAD 和 Azure SQL 身份验证正常工作
- r - 使用两个等长向量的元素之间的所有元素创建新向量
- powershell - 使用powershell替换目录中所有文件的值
- python - 未使用 TensorFlow 或 Keras 分层组织的神经网络
- flutter - 从键盘检索打字输入/坐标信息
- javascript - 无法在 chrome 中安装 Progressive Web App