Android 无法在地图上看到标记
下面是我的MapsActivity.java, 我正在尝试获取当前位置并将标记放在那里。 当我运行应用程序时,我无法看到地图上的标记。出了什么问题? 谢谢你的帮助Android 无法在地图上看到标记,android,google-maps,Android,Google Maps,下面是我的MapsActivity.java, 我正在尝试获取当前位置并将标记放在那里。 当我运行应用程序时,我无法看到地图上的标记。出了什么问题? 谢谢你的帮助 import android.Manifest; import android.content.pm.PackageManager; import android.location.Address; import android.location.Geocoder; import android.location.Location;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.bustracker.usc.myapplication.R;
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 java.io.IOException;
import java.util.List;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LocationManager manager;
private LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("INSIDE ONCREATE", "TRUE");
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);
//get the location service
manager = (LocationManager) getSystemService(LOCATION_SERVICE);
//request the location update thru location manager
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Log.d("BFORE ISPROVIDERENABLED", "TRUE");
if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//get the latitude and longitude from the location
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.d("LATLNG", latitude+" " +longitude);
//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).getSubLocality() + ":";
result += addresses.get(0).getLocality() + ":";
result += addresses.get(0).getCountryCode();
LatLng latLng = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(latLng).title(result));
mMap.setMaxZoomPreference(20);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
} 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) {
}
});
}else if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//get the latitude and longitude from the 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).getSubLocality() + ":";
result += addresses.get(0).getLocality() + ":";
result += addresses.get(0).getCountryCode();
LatLng latLng = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(latLng).title(result));
mMap.setMaxZoomPreference(20);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
} 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) {
}
});
}
}
/**
* 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));
}
@Override
protected void onPause() {
super.onPause();
manager.removeUpdates(locationListener);
Log.i("onPause...","paused");
}
}
导入android.Manifest;
导入android.content.pm.PackageManager;
导入android.location.Address;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.support.v4.app.ActivityCompat;
导入android.support.v4.app.FragmentActivity;
导入android.util.Log;
导入com.bustracker.usc.myapplication.R;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.OnMapReadyCallback;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.MarkerOptions;
导入java.io.IOException;
导入java.util.List;
公共类MapsActivity扩展了FragmentActivity在MapReadyCallback上的实现{
私有谷歌地图;
私人场所经理;
私有位置侦听器位置侦听器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
Log.d(“内部ONCREATE”、“TRUE”);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
//获取定位服务
manager=(位置管理器)getSystemService(位置服务);
//通过位置管理器请求位置更新
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
考虑到呼叫
//ActivityCompat#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于ActivityCompat,请请求权限以获取更多详细信息。
返回;
}
Log.d(“b提供之前”、“真实”);
if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
manager.RequestLocationUpdate(LocationManager.NETWORK_提供程序,0,0,新LocationListener(){
@凌驾
已更改位置上的公共无效(位置){
//从位置获取纬度和经度
双纬度=location.getLatitude();
double longitude=location.getLongitude();
对数d(“纬度”,纬度+“”+经度);
//从纬度和经度获取位置名称
Geocoder Geocoder=新的Geocoder(getApplicationContext());
试一试{
列出地址=
地理编码器。getFromLocation(纬度,经度,1);
字符串结果=addresses.get(0.getSubLocality()+“:”;
结果+=地址.get(0).getLocation()+“:”;
结果+=地址.get(0).getCountryCode();
LatLng LatLng=新LatLng(纬度、经度);
mMap.addMarker(新的MarkerOptions().position(latLng).title(result));
mMap.setMaxZoomPreference(20);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}捕获(IOE异常){
e、 printStackTrace();
}
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
});
}else if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
manager.requestLocationUpdates(LocationManager.GPS_提供程序,0,0,新LocationListener(){
@凌驾
已更改位置上的公共无效(位置){
//从位置获取纬度和经度
双纬度=location.getLatitude();
double longitude=location.getLongitude();
//从纬度和经度获取位置名称
Geocoder Geocoder=新的Geocoder(getApplicationContext());
试一试{
列出地址=
地理编码器。getFromLocation(纬度,经度,1);
字符串结果=addresses.get(0.getSubLocality()+“:”;
结果+=地址.get(0).getLocation()+“:”;
结果+=地址.get(0).getCountryCode();
LatLng LatLng=新LatLng(纬度、经度);
mMap.addMarker(新的MarkerOptions().position(latLng).title(result));
mMap.setMaxZoomPreference(20);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}捕获(IOE异常){
e、 printStackTrace();
}
}
map.addMarker(new MarkerOptions().position(latLng).title(result)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));