【问题标题】:How to get exact view that is set using Latitude and Longitude in android google map如何获取在 android 谷歌地图中使用纬度和经度设置的精确视图
【发布时间】:2014-05-08 11:57:00
【问题描述】:

我正在开发一个 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>

【问题讨论】:

    标签: android google-maps android-layout geolocation


    【解决方案1】:

    你应该 animateCamera 在那个特定的位置标记上喜欢

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

    欲了解更多信息,请访问https://developers.google.com/maps/documentation/android/views#moving_the_camera

    【讨论】:

    • 完美...thnxxx... :) :D
    【解决方案2】:
        @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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多