启动时未加载当前位置

时间:2013-12-19 08:25:15

标签: android performance android-layout android-intent

当我运行应用程序时,它会加载地图但不会加载我当前的位置。当我点击gps之后,它会放大到我当前的位置。我还想在用户从一个位置移动到另一个位置时更新位置。此位置更新代码无法正常工作。当我改变设备的方向时,它会再次重新加载地图。事情再次发生。

Xml文件

 <?xml version="1.0" encoding="utf-8"?>
 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/map"
      android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:name="com.google.android.gms.maps.SupportMapFragment"/>   

类文件

         package com.design.googlemap;

            import android.location.Criteria;
            import android.location.Location;
            import android.location.LocationListener;
            import android.location.LocationManager;
            import android.os.Bundle;
            import android.support.v4.app.FragmentActivity;
            import android.view.Menu;
            import com.google.android.gms.maps.CameraUpdateFactory;
            import com.google.android.gms.maps.GoogleMap;
            import com.google.android.gms.maps.SupportMapFragment;
            import com.google.android.gms.maps.model.LatLng;

            public class MainActivity extends FragmentActivity{
                private GoogleMap googlemap;
                private LocationManager locationManager;
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    SetMapIfNeeded();   
                        }

                private void SetMapIfNeeded() {
                    // TODO Auto-generated method stub
                    if(googlemap==null){
                        //try to obtain google map form SupportMapFragment;
                        googlemap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
                        if(googlemap!= null){
                            SetUpMap();
                        }
                    }
                }

                private void SetUpMap() {
                    // Enable MyLocation Layer of Google Map
                    googlemap.setMyLocationEnabled(true);

                    // Get LocationManager object from System Service Location_SERVICE
                    locationManager =(LocationManager) getSystemService(LOCATION_SERVICE);

                    //Create a criteria object to retrieve provider
                    Criteria criteria= new Criteria();

                    //Get the name of the best provider
                    String provider = locationManager.getBestProvider(criteria,true);

                    //Get Current Location
                    Location myLocation= locationManager.getLastKnownLocation(provider);

                    //Set map type
                    googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

                    //Get Latitude object of current location
                    double latitude=myLocation.getLatitude();

                    //Get Latitude object of current location
                    double longitude=myLocation.getLatitude();

                    //Create a LatLng object for current location
                    LatLng latLng = new LatLng(latitude,longitude);

                    //show the current location of Google Map
                    googlemap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

                    LocationListener listner = new LocationListener() {

                        public void onStatusChanged(String provider, int status, Bundle extras) {
                            // TODO Auto-generated method stub

                        }

                        public void onProviderEnabled(String provider) {
                            // TODO Auto-generated method stub

                        }

                        public void onProviderDisabled(String provider) {
                            // TODO Auto-generated method stub

                        }

                        public void onLocationChanged(Location location) {
                            // TODO Auto-generated method stub

                            locationManager =(LocationManager) getSystemService(LOCATION_SERVICE);
                        }
                    };
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) listner);


                }

                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                    // Inflate the menu; this adds items to the action bar if it is present.
                    getMenuInflater().inflate(R.menu.main, menu);
                    return true;
                }

            }

ManifestFile

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.design.googlemap"
            android:versionCode="1"
            android:versionName="1.0" >

            <uses-sdk
                android:minSdkVersion="12"
                android:targetSdkVersion="17" />

            <permission
                android:name="com.design.googlemap.permission.MAPS_RECEIVE"
                android:protectionLevel="signature" />

             <uses-permission android:name="android.permission.INTERNET" />
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
            <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
            <!--
             The following two permissions are not required to use
             Google Maps Android API v2, but are recommended.
            -->
            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
            <uses-permission android:name="com.design.googlemap.permission.MAPS_RECEIVE" />
            <uses-feature
                android:glEsVersion="0x00020000"
                android:required="true" />

            <application
                android:allowBackup="true"
                android:icon="@drawable/ic_launcher"
                android:label="@string/app_name"
                android:theme="@style/AppTheme" >

                <activity
                    android:name="com.design.googlemap.MainActivity"
                    android:label="@string/app_name" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />

                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
                <meta-data
                    android:name="com.google.android.gms.version"
                    android:value="@integer/google_play_services_version" />
                <meta-data
                    android:name="com.google.android.maps.v2.API_KEY"
                    android:value="XXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
            </application>

        </manifest>

2 个答案:

答案 0 :(得分:1)

1。

如果您只想显示自己的位置,那么MyLocationOverlay可能最简单。

此处的快速信息(该文章的其余部分也可能有用):

http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation

使用MyLocationOverlay,因为它就是这样的。使用MyLocationOverlay和自定义叠加层的Here is a sample application;如果你不需要,你可以随时摆脱自定义的。

2.

为避免重新加载的常见行为,您必须为应用程序处理配置更改。

将此行添加到AndroidManifest.xml。这告诉系统您将自己处理哪些配置更改 - 在这种情况下无需执行任何操作。

android:configChanges="keyboardHidden|orientation|screenSize"

答案 1 :(得分:1)

//Get the name of the best provider
String provider = locationManager.getBestProvider(criteria,false);

即使Gps如果关闭。您的大致位置将被指向。

对于GoogleMaps上的locationUpdates,请查看此link

同样,为了缩放到特定位置,请添加以下代码:

CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(YOUR_LATLNG).zoom(ZOOM_LEVEL).build();     // int ZOOM_LEVEL=12            
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

详细了解link有关缩放的详情。

要隐藏WebView,请调用活动中的“可见性”功能[按下按钮]

WebView wv=(WebView)findViewById(R.id.YOUR_WEB_VIEW);
    wv.setVisibility(View.GONE);// when you want to hide[shrink]
    wv.setVisibility(View.VISIBLE);// when you want to Show[Zoom]