Java 在android studio上使用maps api获取当前位置
我问,如何为android创建一个脚本,用google maps api v2为android获取当前位置。 现在,我的scrpt获取指定cordinate的位置。 我将其发布到我的java文件:Java 在android studio上使用maps api获取当前位置,java,android,google-maps,Java,Android,Google Maps,我问,如何为android创建一个脚本,用google maps api v2为android获取当前位置。 现在,我的scrpt获取指定cordinate的位置。 我将其发布到我的java文件: private GoogleMap mMap; static final LatLng TutorialsPoint = new LatLng(46.493168,11.3306379); private GoogleMap googleMap; @Override protected void
private GoogleMap mMap;
static final LatLng TutorialsPoint = new LatLng(46.493168,11.3306379);
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
Marker TP = googleMap.addMarker(new MarkerOptions().
position(TutorialsPoint).title("TutorialsPoint"));
}
catch (Exception e) {
e.printStackTrace();
}
}
在onCreate()
方法中,键入以下内容:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapfragment);
mapFragment.getMapAsync(this);
使您的活动实现
LocationListener
、onMapReadyCallback
和GoogleAppClient.ConnectionCallbacks
接口。在该接口的方法中:
onMapReady (from onMapReadyCallbacks):
public void onMapReady(GoogleMap map) {
//mapa is your GoogleMap variable
mapa=map;
}
这样做,您可以确保您的地图已准备好工作
在onConnected方法(从GoogleAppClient.ConnectionCallbacks
接口)中,您可以获得您的位置:
@Override
public void onConnected(Bundle bundle) {
Location lkn=LocationServices.FusedLocationApi.getLastLocation(
gApiClient);
if(myLocation==null){
myLocation=new Location("");
}
if(lkn!=null){
myLocation.setLatitude(lkn.getLatitude());
myLocation.setLongitude(lkn.getLongitude());
}
LocationServices.FusedLocationApi.requestLocationUpdates(
gApiClient, mLocationRequest, this);
}
最后,在onLocationChanged中(从LocationListener接口):
因此,每次手机有一个新的位置时,你都会通过这种方式收到它
您还需要实现这两种方法:
//call this method in your onCreate()
protected synchronized void buildGoogleApiClient() {
//gApiClient is a class variable, type GoogleApiClient
gApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i("Map", "Conection failed");
}
})
.addApi(LocationServices.API)
.build();
gApiClient.connect();
createLocationRequest();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
实现
LocationListener
为此,首先你必须确定是否在屏幕上显示谷歌地图视图。然后你可以用记号笔。使用getSupportMapFragment()而不是getFragmentManager()。
//call this method in your onCreate()
protected synchronized void buildGoogleApiClient() {
//gApiClient is a class variable, type GoogleApiClient
gApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i("Map", "Conection failed");
}
})
.addApi(LocationServices.API)
.build();
gApiClient.connect();
createLocationRequest();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}