提问者:小点点

如何在android谷歌地图中获得使用经纬度设置的精确视图


我正在开发一个android应用程序,我在应用程序中使用谷歌地图。我已经为一个特定的位置设置了纬度和经度。但是当我运行我的应用程序时,它不显示我设置了位置的地图。假设我已经设置了孟买的位置,当我启动应用程序时,它应该会显示孟买。但是它显示的是整个世界地图,而不是孟买。以下是我的代码:

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>

共2个答案

匿名用户

您应该< code >动画摄像机在特定的位置标记上,如

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

更多信息请访问https://developers . Google . com/maps/documentation/Android/views # moving _ the _ camera

匿名用户

    @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.