anu*_*anu 1 import android location geolocation locationmanager
我不明白为什么无法解析locationManager.requestLocationUpdates.我搜索了答案,并说它必须导入不再使用的LocationClient.
以下是我找到当前位置方法的方法.请告诉我我是否遗漏了什么.
private void setUpMap() {
// Enable MyLocation Layer of Google Map
mMap.setMyLocationEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria,true);
LocationListener locationChangeListener = new LocationListener() {
public void onLocationChanged(Location l) {
if (l != null) {
Log.i("SuperMap", "Location changed : Lat: " + l.getLatitude() + " Lng: " +
l.getLongitude());
}
}
public void onProviderEnabled(String p) {
}
public void onProviderDisabled(String p) {
}
public void onStatusChanged(String p, int status, Bundle extras) {
}
};
locationManager.requestLocationUpdates(provider,0,0,locationChangeListener);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
// set map type
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Get latitude of the current location
double latitude = myLocation.getLatitude();
// Get longitude of the current location
double longitude = myLocation.getLongitude();
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Show the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located"));
}
Run Code Online (Sandbox Code Playgroud)
}
这是我的进口
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.GeofencingApi;
Run Code Online (Sandbox Code Playgroud)
LocationListener从android.location包导入接口,因为当前从com.google.android.gms.location包导入但requestLocationUpdates方法android.location.LocationListener作为最后一个参数.
变化:
import com.google.android.gms.location.LocationListener;
Run Code Online (Sandbox Code Playgroud)
至
import android.location.LocationListener;
Run Code Online (Sandbox Code Playgroud)