我想谷歌地图显示用户的位置.我尝试了这段代码,但它在Android 6上无效.
private GoogleMap map;
LocationManager lm;
LocationListener ll;
Location l;
LatLng pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_layout);
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
ll = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
l = (Location) location;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.nMap);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = …Run Code Online (Sandbox Code Playgroud) 我想在地图上显示我的位置并放大它.我想使用GoolgeAPIClient.地图渲染和指针创建但位置错误.我觉得即使在调用GoogleAPIClient的onConnected方法之前,也会先执行onMapReady.请帮助这是我的代码.
import android.location.Location;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
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.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
protected static final String TAG = "MainActivity";
/**
* Provides the entry point to Google Play services.
*/
protected GoogleApiClient mGoogleApiClient;
/**
* Represents a geographical location.
*/
protected Location mLastLocation;
protected String mLatitudeLabel;
protected …Run Code Online (Sandbox Code Playgroud) java.lang.SecurityException: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.
Run Code Online (Sandbox Code Playgroud)
该应用程序适用于Android版本5(Lollipop),但在Android版本6上获得安全例外.
我尝试了几个教程,但没有运气获得当前坐标,也许我不能跟随或真的不知道如何设置一个.
抱歉是新手.我只是想创建一个通过gps获取当前位置的应用程序,无论位置是否发生变化.我可以设法获得最后的已知位置但无法实现以获得实时坐标.非常感谢有一个完整的资源来展示解决方案.部分代码我很容易迷失.
先感谢您.
我开发了一个简单的Android应用程序,在这个简单的应用程序中我想获得GPS Latitued和Longitued数据,它在我的模拟器中正常工作但是当我安装在真正的Android设备上时它不起作用.请帮我解决这个问题,以下是我的代码.提前致谢
public class getLocation extends AppCompatActivity {
private Button btnGetLocation;
private TextView textGPS;
private LocationManager locationManager;
private LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_location);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
btnGetLocation = (Button) findViewById(R.id.btnGetLocation);
textGPS = (TextView) findViewById(R.id.textViewGps);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
textGPS.setText("lat: "+location.getLatitude()+" long: "+location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) { …Run Code Online (Sandbox Code Playgroud) 我具有以下函数,可向我返回设备的当前位置:
void getCurrentLocation()
{
Location myLocation = map.getMyLocation();
if(myLocation!=null)
{
double dLatitude = myLocation.getLatitude();
double dLongitude = myLocation.getLongitude();
map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
.title("My Location").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));
}
else
{
Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,这些方法与map有关,可以在onMapReady()函数中使用,但无法显示它。这是为什么?我必须添加哪些库?我这样声明地图:
private MapFragment map;
Run Code Online (Sandbox Code Playgroud) 我试图放大用户在地图上的当前位置.该位置绝对可以访问,因为它在地图上显示为蓝点

但是,GoogleMap.getMyLocation()返回null.
为了启用位置数据,我遵循了Google文档,并在清单中添加了适当的权限,此外还添加了必要的API密钥.
如果它对我的所有MyActivity.java都有帮助:
package com.example.beckah.helloworld;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
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 android.location.Location;
import android.location.LocationListener;
import android.content.Context;
import android.location.LocationManager;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LatLng location;
Location mLocation;
LocationManager locationManager;
@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() …Run Code Online (Sandbox Code Playgroud)