【问题标题】:Android Google Maps v2 not showing Compass and Location IconAndroid Google Maps v2 不显示指南针和位置图标
【发布时间】:2014-08-04 06:44:59
【问题描述】:

我也无法显示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

}

}

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

【问题讨论】:

  • 试试这个..先写这个googleMap.setMyLocationEnabled(true);然后写下你写的两个代码..
  • 好的,这让setMyLocationButton 出现了,谢谢!但是指南针图标仍然没有出现...
  • 我认为指南针图标只有在您将地图旋转到不与北方对齐时才会出现。所以请长按地图并拖动它...然后我认为您将能够看到指南针..

标签: java android google-maps


【解决方案1】:

您还没有启用我的位置层。查看此link 了解更多详情

所以请做

googleMap.setMyLocationEnabled(true);

之前

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

这会让你的setMyLocationButton 出现..

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

【讨论】:

  • 好的,setMyLocationEnabled(true) 很有意义...非常感谢!指南针现在出现
  • 很高兴它有帮助..:)
  • 它没有帮助我..我的类是一个片段而不是一个活动,像这样:public class MyMapFragment extends Fragment implements OnMapReadyCallback, LocationListener {这可能是原因吗?
  • 否 @Supercelo 这不是原因。我也使用过 Fragment,但 getUiSettings()... 帮助了我。
  • 我也没有位置按钮,但后来发现它因为不正确的layout_behavior而隐藏在appbar后面。此评论适用于可能面临像我一样的问题的未来人.....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多