Edd
Edd

Reputation: 3

Google Map Fragment default location

I am new to Android and totally unfamiliar with Google Maps implementation in Android. I have a fragment which is supposed to display Google Maps, it does that, but I want it to open the map of Oslo city by default. I've tried a few things but nothing worked. It just shows the world map somewhere near Africa.

Here is my code:

MapFragment.java

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapFragment extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map_fragment);
    ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
            .getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    setUpMap();
}

private void setUpMap() {
    LatLng latLng = new LatLng(59.95, 10.75);

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    mMap.animateCamera(CameraUpdateFactory.zoomTo(14));

}
}

activity_map_fragment.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>

Upvotes: 0

Views: 1019

Answers (1)

blackkara
blackkara

Reputation: 5052

Try this,

In xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent">
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>

In Code,

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView);
mapFragment.getMapAsync(this);

And last, for camera

LatLng latLng = new LatLng(59.95, 10.75);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));

Upvotes: 1

Related Questions