Android - Google Maps V2 无法使用 Fused Location
Posted
技术标签:
【中文标题】Android - Google Maps V2 无法使用 Fused Location【英文标题】:Android - Google Maps V2 not working with Fused Location 【发布时间】:2017-05-02 19:11:13 【问题描述】:我有这个应用程序,我必须在其中放置一个品脱我的地图并获取我刚刚按下的大头针的纬度和经度,但是当我想单击地图时,应用程序无法识别水龙头,无论如何,这是我的代码:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleMap.OnMapClickListener,
View.OnClickListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener
GoogleMap mMap;
Double latitud, longitud;
Marker marker;
Button buttonAgregarUbicacion;
protected GoogleApiClient mGoogleApiClient;
protected Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
checkGpsEnable(locationManager);
buildGoogleApiClient();
buttonAgregarUbicacion = (Button) findViewById(R.id.button_agregar_ubicacion);
buttonAgregarUbicacion.setOnClickListener(this);
protected synchronized void buildGoogleApiClient()
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
private void checkGpsEnable(LocationManager locationManager)
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Al parecer no tienes el GPS activado, para mejorar la experiencia enciéndolo.")
.setCancelable(false)
.setPositiveButton("Activar GPS",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
Intent callGPSSettingIntent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
);
alertDialogBuilder.setNegativeButton("Cancelar",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
dialog.cancel();
finish();
);
AlertDialog alert = alertDialogBuilder.create();
alert.show();
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.setMyLocationEnabled(true);
if (mLastLocation != null)
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()), 200));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()))
.zoom(17)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
@Override
public void onClick(View view)
switch (view.getId())
case R.id.button_agregar_ubicacion:
if (latitud == null)
Toast.makeText(MapsActivity.this, "Pon un pin en el mapa", Toast.LENGTH_SHORT).show();
else
Intent intent = new Intent(this, CreateReportActivity.class);
intent.putExtra("latitud", latitud);
intent.putExtra("longitud", longitud);
startActivity(intent);
finish();
break;
@Override
public void onConnected( Bundle connectionHint)
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
onMapReady(mMap);
@Override
public void onConnectionSuspended(int i)
mGoogleApiClient.connect();
@Override
public void onConnectionFailed( ConnectionResult connectionResult)
@Override
protected void onStart()
super.onStart();
mGoogleApiClient.connect();
@Override
protected void onStop()
super.onStop();
if (mGoogleApiClient.isConnected())
mGoogleApiClient.disconnect();
@Override
public void onMapClick(LatLng latLng)
mMap.clear();
marker = mMap.addMarker(new MarkerOptions()
.position(latLng).title("¿Aquí?"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
latitud = latLng.latitude;
longitud = latLng.longitude;
无论如何,当我进行调试时,我在方法 onMapClick 中放置断点并没有停在那里,我已经使用它并且工作了,但是直到今天我才实现融合位置,我遇到了这个问题。
如果有人能帮我解决这个问题,我将不胜感激。
谢谢。
【问题讨论】:
【参考方案1】:您忘记为地图设置GoogleMap.OnMapClickListener
。
将此添加到您的onMapReady
:
mMap.setOnMapClickListener(this);
【讨论】:
我现在真的很傻,非常感谢@antonio以上是关于Android - Google Maps V2 无法使用 Fused Location的主要内容,如果未能解决你的问题,请参考以下文章
Google Maps Android API V2 检查 Google Maps 应用程序是不是被禁用
在 Google Maps Android SDK (v2) 中更改建筑物颜色