android - map 上不显示标记

标签 android google-maps

我想获取用户在 map 上的当前位置 当我运行该应用程序时,该应用程序已成功启动,它需要用户的许可才能通过 GPS 获取用户的位置,但 map 上未显示标记。

当我在模拟器上运行此应用程序时,它会显示其他位置,而当我在移动设备上运行此应用程序时,它不会显示位置(标记) 她就是密码。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    LocationManager locationManager;
    LocationListener locationListener;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode ==1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 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);

    }


    /**
     * 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;
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.i("Location::::::", location.toString());
                LatLng person = new LatLng(location.getLatitude(),location.getLongitude());
                mMap.clear();
                mMap.addMarker(new MarkerOptions().position(person).title("person").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(person, 15));

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };
        if(Build.VERSION.SDK_INT < 23){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0 ,locationListener);
        }
        else {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            } else {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                Location lastLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                LatLng loc = new LatLng(lastLoc.getLatitude(), lastLoc.getLongitude());
                mMap.clear();
                mMap.addMarker(new MarkerOptions().position(loc).title("person").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 15));
            }
        }

    }
}

最佳答案

when i run the this app on emulator it shows some where else location

它不显示您当前的位置,因为在使用模拟器时,它的位置是在设置中设置的,它不能确定您的实际位置,所以基本上是手动设置的。

when i run this app on mobile it does not show location(marker)

关于这一点,您可以尝试先在onMapReady中直接调用addMarker。并将您的 onLocationChanged 回调移到 onMapReady 之外。

关于android - map 上不显示标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50432804/

相关文章:

android - 从 .APK 文件压缩到 SD 卡

android - 连接蓝牙设备失败

android - 从我的 android 应用程序将新文档上传到 Google 驱动器

javascript - 将 JSON 文件中的数据加载到 Google map 中的 map 标记中

android - Webview 中的声音

android - 安卓手机指纹API

android - "The constructor Geocoder(Context, Locale) is undefined"?

javascript - Angular $http.post 谷歌地图Javascript API错误

javascript - 加载时从谷歌地图中的字段获取值

javascript - 调整大小后 Google map 不会更新