gpt4 book ai didi

Android:无法在 map 上获取当前位置

转载 作者:搜寻专家 更新时间:2023-11-01 08:03:49 24 4
gpt4 key购买 nike

我有一个带有谷歌地图的应用程序,其中有一个显示 map 的 Activity 。我有一个允许我更改 map 类型的菜单,但我希望有一个选项来获取我的当前位置。

这是我的 Activity 代码:

package com.example.chiapa_mapas;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
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;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends android.support.v4.app.FragmentActivity implements OnMapClickListener, LocationListener{

private GoogleMap mMap;
private Context ctx;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setOnMapClickListener(this);


// Enable LocationLayer of Google Map
mMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);


}

@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.opcoes, menu);
return true; }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {

case R.id.normal:
mMap.setMapType(1);
break;

case R.id.satellite:
mMap.setMapType(2);
break;

case R.id.terrain:
mMap.setMapType(3);
break;
case R.id.hybrid:
mMap.setMapType(4);
break;

case R.id.none:
mMap.setMapType(0);
break;

case R.id.posicao_actual:
//mMap.setMapType(0);
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria,true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}

locationManager.requestLocationUpdates(provider, 20000, 0,(android.location.LocationListener) ctx);

break;

default: return super.onOptionsItemSelected(item);
}

return true;
}

@Override
public void onMapClick(LatLng position) {
// TODO Auto-generated method stub

mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));

//double ltt = position.latitude;
//double lgt = position.longitude;

//String msg = "Latitude: " + Double.toString(ltt) + " , Longitude: " + Double.toString(lgt) + "";

//Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
//toast.show();
}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView

locationManager.removeUpdates((android.location.LocationListener) this);

}

}

有人可以帮忙吗?当我按下选项“posicao actual”(当前位置)时,它崩溃了。提前致谢恰帕

最佳答案

这对我有用:

    public void methodName(){
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
android.location.LocationListener locationListener = new android.location.LocationListener() {
public void onLocationChanged(Location location) {
//Any method here
}
public void onStatusChanged (String provider, int status, Bundle extras){}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled (String provider){}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, locationListener);
}

谢谢恰帕。

关于Android:无法在 map 上获取当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17138526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com