Android 谷歌地图无法放大到当前位置
我正试图在android应用程序上放大我的当前位置。地图仍然默认为非洲。不会放大到我的当前位置 参考答案后编辑代码。仍然无法获得GPS值Android 谷歌地图无法放大到当前位置,android,google-maps,Android,Google Maps,我正试图在android应用程序上放大我的当前位置。地图仍然默认为非洲。不会放大到我的当前位置 参考答案后编辑代码。仍然无法获得GPS值 public class MainActivity extends Activity { private GoogleMap googleMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedI
public class MainActivity extends Activity {
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location == null){
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double lat= location.getLatitude();
double lng = location.getLongitude();
LatLng ll = new LatLng(lat, lng);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 20));
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {}
};
Log.d("APpln", "well hello there >>"+location);
locationManager.requestLocationUpdates(provider, 20000, 0, locationListener);
}else{
double lat= location.getLatitude();
double lng = location.getLongitude();
LatLng ll = new LatLng(lat, lng);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 20));
}
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
private void makeUseOfNewLocation(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng ll = new LatLng(lat, lng);
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + lat + "\nLong: " + lng, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
Manifext.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pcakage"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<uses-permission
android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature android:glEsVersion="0x00020000"/>
<uses-feature android:required="true"/>
<!-- Goolge Maps API Key -->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="package.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="some_value" />
</application>
</manifest>
我不确定我会错在哪里。当我在lat和long上打印值时,我没有得到任何值。这可能意味着
Location Location=locationManager.getLastKnownLocation(提供程序)
无法获取位置。i与访问有关吗?有时您无法从locationManager.getLastKnownLocation(提供者)获取有效位置代码>
启动位置侦听器,并在获得位置时移动相机
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location == null){
LocationListener locationListener = new LocationListener() {
void onLocationChanged(Location location) {
double lat= location.getLatitude();
double lng = location.getLongitude();
LatLng ll = new LatLng(lat, lng);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 20));
}
LocationManager.requestLocationUpdates(provider, 20000, 0, locationListener);
}else{
double lat= location.getLatitude();
double lng = location.getLongitude();
LatLng ll = new LatLng(lat, lng);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 20));
}
}
}
有时,您可能无法从locationManager.getLastKnownLocation(提供者)获取有效位置代码>
启动位置侦听器,并在获得位置时移动相机
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location == null){
LocationListener locationListener = new LocationListener() {
void onLocationChanged(Location location) {
double lat= location.getLatitude();
double lng = location.getLongitude();
LatLng ll = new LatLng(lat, lng);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 20));
}
LocationManager.requestLocationUpdates(provider, 20000, 0, locationListener);
}else{
double lat= location.getLatitude();
double lng = location.getLongitude();
LatLng ll = new LatLng(lat, lng);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 20));
}
}
}
相反,要使用标准的最佳提供商,请尝试手动获得更好的提供商(sea)
重点放在地图上
private void focusMapOnCurrentLocation()
{
final int DURATIONMS_CONST = 2000;
final int ZOOM_ANIM_CAMERA_CONST = 15;
final int ZOOM_MOVE_CAMERA_CONST = 20;
// Set focus on current location on google map;
Location location = getCurrentLocation();
double lat = location.getLatitude();
double log = location.getLongitude();
@Nonnull LatLng latLng = new LatLng(lat, log);
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, ZOOM_MOVE_CAMERA_CONST));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(ZOOM_ANIM_CAMERA_CONST), DURATIONMS_CONST, null);
}
同样,您可以使用mGoogleMap.setMyLocationEnabled(true) 要使用标准的最佳提供商,请尝试手动获得更好的提供商(sea)
重点放在地图上
private void focusMapOnCurrentLocation()
{
final int DURATIONMS_CONST = 2000;
final int ZOOM_ANIM_CAMERA_CONST = 15;
final int ZOOM_MOVE_CAMERA_CONST = 20;
// Set focus on current location on google map;
Location location = getCurrentLocation();
double lat = location.getLatitude();
double log = location.getLongitude();
@Nonnull LatLng latLng = new LatLng(lat, log);
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, ZOOM_MOVE_CAMERA_CONST));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(ZOOM_ANIM_CAMERA_CONST), DURATIONMS_CONST, null);
}
同样,您可以使用mGoogleMap.setMyLocationEnabled(true) 您是否添加了获取用户位置的权限?i、 事实上是的。我已经添加了我的manifext。xml@misguided你知道位置了吗?请你把它记录下来好吗。getLastKnownLocation似乎为null您是对的位置为null
。但我不知道为什么。@当您的设备没有最后一个位置时,很多时候都会出现误导。解决方案是使用requestLocationUpdate和implementLocationListener,它们提供了方法onLocationChanged,它将提供更新的位置。因此,您可以在这里设置动画/缩放位置。您是否添加了获取用户位置的权限?i、 事实上是的。我已经添加了我的manifext。xml@misguided你知道位置了吗?请你把它记录下来好吗。getLastKnownLocation似乎为null您是对的位置为null
。但我不知道为什么。@当您的设备没有最后一个位置时,很多时候都会出现误导。解决方案是使用requestLocationUpdate和implementLocationListener,它们提供了方法onLocationChanged,它将提供更新的位置。所以在这里您可以设置动画/缩放locationlocationListener不起作用,如果没有使用所有实现的方法,那么我已经实现了相同的方法。但是当我放置LocationManager.requestLocationUpdates(provider,20000,0,locationListener)
时会出错。你能提供建议吗?我刚刚注意到有一个拼写错误,是locationManager。requestLocationUpdates(provider,20000,0,locationListener)Mate已经更新了代码,但是仍然看不到设备放大到当前gps位置您正在为您创建的第二个locationListener请求位置更新…您能看到您的toast消息吗?不,我看不到我的toast消息。如果未使用所有实现的方法,locationListener无法工作。我已经实现了相同的方法。但是当我放置LocationManager.requestLocationUpdates(provider,20000,0,locationListener)
时会出错。你能提供建议吗?我刚刚注意到有一个拼写错误,是locationManager。requestLocationUpdates(provider,20000,0,locationListener)Mate已经更新了代码,但是仍然看不到设备放大到当前gps位置您正在请求为您创建的第二个locationListener更新位置…您能看到您的toast消息吗?不,我看不到我的toast消息。