使用我当前的位置启动地图应用程序
Start the map app with my current location
这是我的Activity:
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
addMaracanazinho();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void addMaracanazinho()
{
LatLng pos = new LatLng(-34.642491, -58.642841);
mMap.addMarker(new MarkerOptions()
.title("Maracanazinho")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
.position(pos));
}
private void setUpMapIfNeeded()
{
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null)
{
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
{
mMap.setMyLocationEnabled(true);
}
}
}
}
我希望我的应用程序以我当前位置的缩放开始。我寻找了一些代码,但没有人有我想要的答案。有人可以告诉我应该添加什么吗?
我找到这个:How would I make my google maps app start with zoom on my current location
但我不明白如何将其放入我的代码中。
谢谢,对不起我的英语不好。
用过animateCamera
喜欢
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
latitude, longitude), 18.0f));
将您的 setUpMapIfNeeded 方法更改为以下内容并添加其余代码。基本上,您需要向系统的位置管理器注册一个 LocationListener。您会收到回调 onLocationChanged(Location l)。这个 Location l 是用户的位置,然后你将这个新位置设置到你的地图上。简单。
private void setUpMapIfNeeded()
{
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null)
{
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
{
mMap.setMyLocationEnabled(true);
}
}
}
private LocationListener locationListener = new MyLocationListener(){
@Override
public void onLocationChanged(Location location) {
// called when the listener is notified with a location update from the GPS
LatLng userPosition = new LatLng(location.getLatitude(),
location.getLongitude());
if (mMap != null){
mMap .moveCamera(CameraUpdateFactory.newLatLngZoom(userPosition,
15));
}
}
@Override
public void onProviderDisabled(String provider) {
// called when the GPS provider is turned off (user turning off the GPS on the phone)
}
@Override
public void onProviderEnabled(String provider) {
// called when the GPS provider is turned on (user turning on the GPS on the phone)
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// called when the status of the GPS provider changes
}
};
在您的 MapsActivity 文件中 -> 更改 setUpMapIfNeeded() 函数:
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private LocationManager locationManager;
private String provider;
...
private void setUpMapIfNeeded()
{
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null)
{
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
{
mMap.setMyLocationEnabled(true);
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);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(myLocation.getLatitude(), myLocation.getLongitude()))
.zoom(14).build();
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
}
}
也可以通过设置数字来设置Zoom(这里Zoom为14)
这是我的Activity:
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
addMaracanazinho();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void addMaracanazinho()
{
LatLng pos = new LatLng(-34.642491, -58.642841);
mMap.addMarker(new MarkerOptions()
.title("Maracanazinho")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
.position(pos));
}
private void setUpMapIfNeeded()
{
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null)
{
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
{
mMap.setMyLocationEnabled(true);
}
}
}
}
我希望我的应用程序以我当前位置的缩放开始。我寻找了一些代码,但没有人有我想要的答案。有人可以告诉我应该添加什么吗?
我找到这个:How would I make my google maps app start with zoom on my current location 但我不明白如何将其放入我的代码中。 谢谢,对不起我的英语不好。
用过animateCamera
喜欢
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
latitude, longitude), 18.0f));
将您的 setUpMapIfNeeded 方法更改为以下内容并添加其余代码。基本上,您需要向系统的位置管理器注册一个 LocationListener。您会收到回调 onLocationChanged(Location l)。这个 Location l 是用户的位置,然后你将这个新位置设置到你的地图上。简单。
private void setUpMapIfNeeded()
{
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null)
{
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
{
mMap.setMyLocationEnabled(true);
}
}
}
private LocationListener locationListener = new MyLocationListener(){
@Override
public void onLocationChanged(Location location) {
// called when the listener is notified with a location update from the GPS
LatLng userPosition = new LatLng(location.getLatitude(),
location.getLongitude());
if (mMap != null){
mMap .moveCamera(CameraUpdateFactory.newLatLngZoom(userPosition,
15));
}
}
@Override
public void onProviderDisabled(String provider) {
// called when the GPS provider is turned off (user turning off the GPS on the phone)
}
@Override
public void onProviderEnabled(String provider) {
// called when the GPS provider is turned on (user turning on the GPS on the phone)
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// called when the status of the GPS provider changes
}
};
在您的 MapsActivity 文件中 -> 更改 setUpMapIfNeeded() 函数:
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private LocationManager locationManager;
private String provider;
...
private void setUpMapIfNeeded()
{
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null)
{
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
{
mMap.setMyLocationEnabled(true);
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);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(myLocation.getLatitude(), myLocation.getLongitude()))
.zoom(14).build();
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
}
}
也可以通过设置数字来设置Zoom(这里Zoom为14)