Question

I am developing an android application in which I am using google maps in app. I have set latitude and longitude for a particular location. But when I run my application, it don't displays the map that I have set the location. Suppose I've set location for Mumbai,it should display Mumbai when I start app. But it is displaying whole world map instead of Mumbai. Here is my code :

public class ContactUs extends Activity 
{
    TextView t1,t2;
    // latitude and longitude
     double latitude = 19.071731;
     double longitude =72.906085;
     private GoogleMap googleMap;
     MapView mapView;
     String name="XYZ Location";
     String add="Address of Location";
     String complete=name+"\n"+add;

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_contact_us);
            t1=(TextView)findViewById(R.id.textView1);
            t1.setShadowLayer(1,0,0,color.black);

            try {
                // Loading map
                initilizeMap();

            } catch (Exception e) {
                e.printStackTrace();
            }
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            googleMap.setMyLocationEnabled(true);
         // create marker
            MarkerOptions marker = new MarkerOptions()
                        .position(new LatLng(latitude, longitude)).title(complete);
            googleMap.addMarker(marker);
            marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
            googleMap.getUiSettings().setAllGesturesEnabled(true);
            googleMap.setTrafficEnabled(true);
            //mapView=(MapView)findViewById(R.id.map);
        }

        /**
         * function to load map. If map is not created it will create it for you
         * */
        @SuppressLint("NewApi")
        private void initilizeMap() {
            if (googleMap == null) {
                googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

                // check if map is created successfully or not
                if (googleMap == null) {
                    Toast.makeText(getApplicationContext(),
                            "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
                }
            }
        }

        @Override
        protected void onResume() {
            super.onResume();
            initilizeMap();
        }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

    <LinearLayout 
         android:orientation="vertical"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/address1"
        android:textStyle="bold"
        android:textSize="18sp"
        android:layout_marginBottom="5dp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:text="@string/address2"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/address3" 
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textStyle="bold"
        android:textSize="18sp"
        android:text="@string/get" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textStyle="bold"
        android:text="@string/central" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="@string/address4" />

        </LinearLayout>  
    </ScrollView>

    <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment"/>
    </RelativeLayout>
</LinearLayout>
Was it helpful?

Solution

You should animateCamera on that particular location Marker like

 LatLng mumbai= new LatLng(lat, lng);
 mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mumbai, 18.0f));

For more information go to https://developers.google.com/maps/documentation/android/views#moving_the_camera

OTHER TIPS

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mContext = getActivity();
    mMap = getMap();
    mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
    mMap.addTileOverlay(new TileOverlayOptions().tileProvider(new VexLocalTileProvider(getResources().getAssets())));
    CameraUpdate upd = CameraUpdateFactory.newLatLngZoom(new LatLng(41.87145, 12.52849), 14);
    mMap.moveCamera(upd);
    mOverscrollHandler.sendEmptyMessageDelayed(0,100);
}

you have another option to fix map area by

Disabled built-in GoogleMap's gestures.
Checking for allowed area when processing events.
Set bounds/zoom manually with standard Map's functions.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top