0
我嘗試編寫示例代碼來測試我的應用程序中的Google地圖API。一切看起來不錯,但是當我從我的應用程序打開Google地圖時,它停止工作。安卓谷歌地圖得到正確的位置不工作
請給我一些幫助。
public class SmarttreenearbyActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smarttreenearby);
// 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);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
// Add a marker in Sydney and move the camera
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
double y = location.getLatitude();
double x = location.getLongitude();
LatLng sydney = new LatLng(x,y);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
嗯..我想知道我的代碼發生了什麼:)但仍然謝謝你。 – LeonSaber
我在開發的一些應用程序上使用了GoogleMaps。我強烈建議你遵循@TheSunny推薦。請使用此https://developers.google.com/maps/documentation/android-api/location?hl=pt-br –
非常感謝:) – LeonSaber