我尝试制作一个具有地图活动的应用,一旦打开,地图就会从用户的当前位置开始。我尝试使用指南和在线帮助,但似乎所有这些指南都已过时,因为它们默认情况下在map活动中有onResume和onStart方法,而是我有onMapReady方法而不是这些方法。这是我尝试编写的代码,但它不起作用(应用程序崩溃),
package com.imt.civilwatch;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.text.StringCharacterIterator;
public class MapActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleMap mMap;
protected GoogleApiClient mGoogleApiClient;
Location mLastLocation;
private double mLatitudeText;
private double mLongitudeText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(32, 35);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
//mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
@Override
public void onConnected(Bundle connectionHint) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText = (Double.valueOf(mLastLocation.getLatitude()));
mLongitudeText = (Double.valueOf(mLastLocation.getLongitude()));
LatLng last = new LatLng( mLatitudeText,mLongitudeText);
mMap.moveCamera(CameraUpdateFactory.newLatLng(last));
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
答案 0 :(得分:0)
方法 onMapReady 在地图的片段准备就绪并显示地图时运行。
onConnected 方法在设备成功连接Google Play位置服务并且(如果可用)获取您的上一个已知位置时运行。
如果您不希望相机移动到当前位置,请删除此条件之间的最新信息:
if(mLastLocation != null){...}
对于地图,相机是屏幕上可见的。方法mMap.moveCamera(...)告诉Map将视图置于特定LatLng的中心。
如果您的手机在使用Google Play位置API连接之前就已准备就绪,则当前位置可见:
if (mLastLocation != null) {
mLatitudeText = (Double.valueOf(mLastLocation.getLatitude()));
mLongitudeText = (Double.valueOf(mLastLocation.getLongitude()));
LatLng last = new LatLng( mLatitudeText,mLongitudeText);
mMap.moveCamera(CameraUpdateFactory.newLatLng(last));
}
希望我很清楚。
请参阅CameraFactory文档,了解地图上的相机操作:https://developers.google.com/android/reference/com/google/android/gms/maps/CameraUpdateFactory
答案 1 :(得分:0)
从谷歌地图Android API的documentation,Android设备可用的位置数据包括使用技术组合确定的设备的当前位置 - 移动的方向和方法,以及设备是否具有移动到预定义的地理边界或地理围栏。
根据应用程序的需要,您可以选择以下选项:
使用“我的位置”图层提供一种简单的方式来显示 设备在地图上的位置。它不提供数据。
使用Google Play services Location API是 推荐用于所有针对位置数据的程序化请求。
并使用 LocationSource 界面允许您提供自定义位置提供程序。
有关更多信息,请查看本教程,了解如何获取当前位置:
答案 2 :(得分:-1)
您需要GoogleApiClient和LocationRequest,以及清单两个用户权限,即“访问精细位置和访问粗略位置”,并获取当前位置您需要执行以下操作: 在清单中包含此内容: -
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
- &GT; Acticity中的当前位置代码将如下所示: - &GT;在活动中全局创建这两个变量:
GoogleApiClient mGoogleapiClient;
LocationRequest mUserLocationRequest;
- &GT;然后来到@override Onconnected()并执行以下操作: -
@Override
public void onConnected(Bundle bundle) {
mUserLocationRequest = LocationRequest.create();
/* Priority = High so that more Correct User Location will be Accessed
up to date information ot highest information get*/
/* Priority = Low if you Don't Want The Exect Location Of The User */
mUserLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
/*For every 1 min I have to Fetch My User Location*/
mUserLocationRequest.setInterval(1000);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleapiClient, mUserLocationRequest, (LocationListener) MainActivity.this);
}
- &GT; Now Impelents Activity中的LocationListener实现了Method onLocationChanged():并在此处执行此类操作,只要用户更改其位置,就会为您提供最新信息: -
@Override
public void onLocationChanged(Location location) {
if (location == null)
{
Toast.makeText(this, "Sorry We Can't able to Get Your Current Location", Toast.LENGTH_SHORT).show();
}
else
{
LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng,15);
mGoogleMap.animateCamera(update);
}
}