Java Android Google Maps v2 不显示指南针和位置图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24209861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:50:11  来源:igfitidea点击:

Android Google Maps v2 not showing Compass and Location Icon

javaandroidgoogle-maps

提问by user2127726

I am not able to show the compassicon and the my locationicon either. I have the code googleMap.getUiSettings.setMyLocationButtonEnabled(true)and googleMap.getUiSettings().setCompassEnabled(true);but it is not showing on the maps.

我也无法显示compass图标和my location图标。我有代码googleMap.getUiSettings.setMyLocationButtonEnabled(true)googleMap.getUiSettings().setCompassEnabled(true);但它没有显示在地图上。

package com.ctc.weathermap;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;

public class WeatherMapActivity extends Activity implements LocationListener {

private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.weather_maps_main);

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);

    boolean enabledGPS = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean enabledWiFi = service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (!enabledGPS) {
        Toast.makeText(this, "GPS signal not found", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }
    else if(!enabledWiFi) {
           Toast.makeText(this, "Network signal not found", Toast.LENGTH_LONG).show();
           Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
           startActivity(intent);
    }

    initializeMap();
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);
    googleMap.getUiSettings().setCompassEnabled(true);
}

private void initializeMap() {
    // check if map is created
    if(googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); // creates the map

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Map could not be created", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    initializeMap();
}

@Override
public void onLocationChanged(Location location) {
    googleMap.clear();
    MarkerOptions marker = new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude()));
    marker.title("Current location");   
    marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));   
    googleMap.addMarker(marker);
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16));
}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

}

}

The rest of the code works such as markers and what not. I have tried putting the code for location and compass in initializeMap()but it still does not show up. I would appreciate any help. Thank you!

其余的代码可以工作,例如标记等等。我试过把位置和指南针的代码放进去,initializeMap()但它仍然没有显示出来。我将不胜感激任何帮助。谢谢!

采纳答案by Lal

You havent enabled the my-location layer.Check this linkfor more details

您尚未启用 my-location 图层。查看此链接了解更多详情

So please do

所以请做

googleMap.setMyLocationEnabled(true);

before

googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);

This will make your setMyLocationButtoncome up..

这会让你setMyLocationButton上来..

And,the compass icon will appear only if you rotate the map to not align to north.

而且,仅当您将地图旋转到不与北对齐时才会出现指南针图标