我想要一个标记来显示我的当前位置.添加所需的所有权限.当我注释掉mMap.addMarker和mMap.moveCamera时,该应用程序正在运行并显示Googlemaps.如果我在代码中使用这两个之一,则该应用会在地图打开之前崩溃.
我尝试删除不为null的标记,但这不能解决问题.
你们知道我如何使该应用程序正常工作吗?
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {private GoogleMap mMap;private List<LatLng> fountain = null;private LocationManager locationManager;private double posLat;private double posLng;private LatLng position;private Marker mposition;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) this.getSystemService(Context.LOCATION_SERVICE); startGPS();}@OverrIDepublic voID onMapReady(GoogleMap GoogleMap) { mMap = GoogleMap; // Add a marker in Sydney and move the camera LatLng sydney = new LatLng(48.16786112327462, 16.383984438313828); mposition = mMap.addMarker(new MarkerOptions().position(sydney).Title("Your position").icon(BitmapDescriptorFactory.fromresource(R.drawable.location))); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));}//--------------------------------------------GPS Listener---------------------------------------public voID startGPS() { 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) { ActivityCompat.requestPermissions(this, new String[]{androID.Manifest.permission.ACCESS_FINE_LOCATION}, 5); } locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, this); onLocationChanged(locationManager.getLastKNownLocation(LocationManager.GPS_PROVIDER));}@OverrIDepublic voID onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 5: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { } else { getDialog2("Keine Erlaubnis für GPS").show(); } } }}@OverrIDepublic voID onLocationChanged(Location location) { posLat = location.getLatitude(); posLng = location.getLongitude(); position = new LatLng(posLat, posLng); if (mposition != null) { mposition.remove(); } mposition = mMap.addMarker(new MarkerOptions().position(position).Title("Your position"). icon(BitmapDescriptorFactory.fromresource(R.drawable.location))); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 11));}@OverrIDepublic voID onStatusChanged(String provIDer, int status, Bundle extras) {}@OverrIDepublic voID onProvIDerEnabled(String provIDer) {}@OverrIDepublic voID onProvIDerDisabled(String provIDer) {}
// —————————-辅助方法—————— —————————–
public Dialog getDialog2(String string) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(string); builder.setPositivebutton("OK", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { finish(); } }); return builder.create();}public Dialog getDialog(String string) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(string); builder.setPositivebutton("OK", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); return builder.create();}
}
解决方法:
好的,我已经解决了问题.所以我在这里发布解决方案.
我已经在MapsActivity上实现了LocationListener接口,由于某种原因,它无法以这种方式工作.我可以检索地理坐标,但是只要我想移动相机或添加标记,应用就会在打开时崩溃.
我不知道为什么,但是:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000、5,此)
我撤消了LocationListener的实现,只是在“,this”的位置创建了一个新的:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, new LocationListener() { @OverrIDe public voID onLocationChanged(Location location) { posLat = location.getLatitude(); posLng = location.getLongitude(); position = new LatLng(posLat, posLng); if (mposition != null) { mposition.remove(); } mposition = mMap.addMarker(new MarkerOptions().position(position).Title("Your position"). icon(BitmapDescriptorFactory.fromresource(R.drawable.location))); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 11)); } @OverrIDe public voID onStatusChanged(String provIDer, int status, Bundle extras) { } @OverrIDe public voID onProvIDerEnabled(String provIDer) { } @OverrIDe public voID onProvIDerDisabled(String provIDer) { } } );
这样,它可以毫无问题地工作.
总结以上是内存溢出为你收集整理的Android-Google Maps-LocationListener-在onLocationChanged上添加标记会使应用崩溃全部内容,希望文章能够帮你解决Android-Google Maps-LocationListener-在onLocationChanged上添加标记会使应用崩溃所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)