Issue
I am trying to display a location with a MapView but I can't get it to work. I have already look into all the other threads with the same questions but the solutions don't work for me.
Here is what is supposed to work but doesn't:
public void displayMap(){
final MapView mapView = findViewById(R.id.locationMapView);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng coordinates = new LatLng(14.0583, 108.2772); //for testing
googleMap.addMarker(new MarkerOptions().position(coordinates).title("Test"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 15));
}
});
}
I am new to Android Studio and just trying things out. I also already tried to comprehend the reference/documentation but it was not that helpful because I don't really get where to start.
I'd be very thankful if someone explained the mistake to me. Of course, if I missed important information you need I'll get it for you.
edit1:
this is the logcat
Solution
I stopped trying to implement this as a MapView and switched to a MapAcitvity. Implementing this is much easier it seems, just create "new-> Google-> Map Activity" in Android Studio
I think the issue with my approach was that I didn't know where to put the api key. although it didnt thow an error concerning this...
EDIT:
Here is the solution / an example of how I got it to work:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mMap = findViewById(R.id.map);
mMap.onCreate(savedInstanceState);
mMap.getMapAsync(this);
}
@Override
public void onStart() {
MapView mMap = findViewById(R.id.map);
super.onStart();
mMap.onStart();
}
@Override
public void onResume() {
MapView mMap = findViewById(R.id.map);
super.onResume();
mMap.onResume();
}
@Override
public void onPause() {
MapView mMap = findViewById(R.id.map);
super.onPause();
mMap.onPause();
}
@Override
public void onStop() {
MapView mMap = findViewById(R.id.map);
super.onStop();
mMap.onStop();
}
@Override
public void onLowMemory() {
MapView mMap = findViewById(R.id.map);
super.onLowMemory();
mMap.onLowMemory();
}
@Override
public void onDestroy() {
MapView mMap = findViewById(R.id.map);
super.onDestroy();
mMap.onDestroy();
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
LatLng Test = new LatLng(50, 9);
mMap.animateCamera(CameraUpdateFactory.newCameraPosition( new CameraPosition.Builder().target(Test).zoom(15.0f).build()),20000, null);
}
I won't include the xml code, as it is just a fill_parent mapView called "map".
I put the API Key in the AndroidManifest.xml like this:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
I think thats it. Thanks for the help and tips!
Answered By - unkn0wn.a
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.