簡體   English   中英

Android Google Maps v2未顯示指南針和位置圖標

[英]Android Google Maps v2 not showing Compass and Location Icon

我無法顯示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()但它仍然沒有顯示出來。 我將不勝感激任何幫助。 謝謝!

您尚未啟用my-location圖層。請查看此鏈接以獲取更多詳細信息

所以請做

googleMap.setMyLocationEnabled(true);

之前

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

這將使你的setMyLocationButton出現..

並且, 僅當您將地圖旋轉到不與北方對齊時,才會顯示羅盤圖標

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM