我正在尝试在谷歌地图android当前位置缩放相机,但我的代码不工作我已经尝试了很多方法我的gps位置在手机上这个代码在模拟器上工作,但没有在设备上工作我试过onLocationChanged方法也请任何好友建议我一个正确的工作解决方案
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, mLocationListener);
Criteria mCriteria = new Criteria();
String bestProvider = String.valueOf(mLocationManager.getBestProvider(mCriteria, true));
Location mLocation = mLocationManager.getLastKnownLocation(bestProvider);
if (mLocation != null) {
Log.e("TAG", "GPS is on");
final double currentLatitude = mLocation.getLatitude();
final double currentLongitude = mLocation.getLongitude();
LatLng loc1 = new LatLng(currentLatitude, currentLongitude);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLatitude, currentLongitude), 10));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 1000, null);
答案 0 :(得分:2)
@Component({
selector: 'child-comp',
template: `
<input type="button" value="click me" (click) = [funcName]()> // Here I am calling the function which is assign to a variable
`,
})
export class ChildComp {
funcName: string;
constructor() {
funcName = 'dummyFunction'; // assigning function name to variable
}
}
答案 1 :(得分:1)
几个小时后,我做了一个实时跟踪GPS的课程。 首先,将此类添加到项目中
public class GPSTracker implements LocationListener {
private final Activity mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000*60; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
private LocationCallbackListener mCallbackListener;
public GPSTracker(Activity context, LocationCallbackListener callbackListener) {
this.mContext = context;
this.mCallbackListener = callbackListener;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
this.canGetLocation = true;
}else {
this.canGetLocation = false;
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
this.canGetLocation = true;
if (location == null) {
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}else {
this.canGetLocation = false;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS() {
if (locationManager != null) {
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
locationManager.removeUpdates(GPSTracker.this);
}
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
mCallbackListener.onChangeLocation(location.getLatitude(),location.getLongitude());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}}
在你的片段或活动中,在MapView的初始化旁边声明它:
public GPSTracker mGPS;
mGPS = new GPSTracker(mActivity, new LocationCallbackListener() {
@Override
public void onChangeLocation(double latitude, double longitude) {
}
});
每当您的位置发生变化时,它都会以onChangeLocation()
覆盖方式更新。
<强>加成强>
如果您想要访问当前的纬度,经度。只需要打电话
mGPS.getLatitude();
或mGPS.getLongitude();
希望这个会更进一步帮助你。
答案 2 :(得分:0)
替换:
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLatitude, currentLongitude), 10));
使用:
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(loc1, 10));