android google map v2新的定位方法
阿新 • • 發佈:2018-12-10
package com.diecolor;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.maps.MapController;
public class MainActivity extends FragmentActivity implements LocationListener {
MapFragment mapFragment;
GoogleMap gMap;
MapController mapController;
LocationManager location;
Location location2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment sMapFragment = (SupportMapFragment) this
.getSupportFragmentManager().findFragmentById(R.id.map);
gMap = sMapFragment.getMap();
gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
gMap.setTrafficEnabled(true);
// lmocation=LocationManager
gMap.setMyLocationEnabled(true);
location = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
@Override
protected void onResume() {
location.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 100,
10, this);
super.onResume();
}
@Override
protected void onPause() {
location.removeUpdates(this);
super.onPause();
}
@Override
public void onLocationChanged(Location location) {
if (location == null) {
Toast.makeText(this, "木有定位", 1000).show();
} else {
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
gMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.maps.MapController;
public class MainActivity extends FragmentActivity implements LocationListener {
GoogleMap gMap;
MapController mapController;
LocationManager location;
Location location2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment sMapFragment = (SupportMapFragment) this
gMap = sMapFragment.getMap();
gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
gMap.setTrafficEnabled(true);
// lmocation=LocationManager
gMap.setMyLocationEnabled(true);
location = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
@Override
protected void onResume() {
location.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 100,
10, this);
super.onResume();
}
@Override
protected void onPause() {
location.removeUpdates(this);
super.onPause();
}
@Override
public void onLocationChanged(Location location) {
if (location == null) {
Toast.makeText(this, "木有定位", 1000).show();
} else {
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
gMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}