【问题标题】:current location is not loading at Startup当前位置未在启动时加载
【发布时间】:2013-12-19 08:25:14
【问题描述】:

当我运行应用程序时,它会加载地图,但不会加载我当前的位置。当我点击 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;
                }

            }

清单文件

        <?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>

【问题讨论】:

标签: android performance android-layout android-intent


【解决方案1】:
//Get the name of the best provider
String provider = locationManager.getBestProvider(criteria,false);

即使 Gps 关闭。系统会指出您的大概位置。

对于您的 GoogleMaps 上的位置更新,请通过 link

对于缩放到特定位置添加此代码:

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

通过此link 了解有关缩放的更多信息。

要隐藏 WebView,请在 Activity[OnClick of your Button] 中调用 Visibility 函数

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]

【讨论】:

  • 它指向我当前的位置,但我想表明当用户打开我的应用程序时它会自动放大到指向的位置
  • 我已经更新了我的答案。希望这是你想要的!
  • 如果我的回答对你有用..那么你可以接受我的回答并关闭你的问题。否则您的问题将无法回答
  • 先生,我在地图上添加了一个按钮,当用户单击该按钮时,地图将转移到屏幕的 3/4 部分,其余部分填充包含 webview。如果用户单击地图将缩放到特定级别,并且在 webview 情况下相同。先生,我是否必须再次声明地图,或者我可以将前一个地图包含到下一个活动中。请指导我怎么做。
  • 这种收缩和扩展可以在同一个Activity中完成。我觉得不需要创建任何其他活动。只需使用动画并进行缩小和缩放。是的,您可以再次使用相同的地图视图
【解决方案2】:

1.

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

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

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

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

2.

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

将此行添加到您的 AndroidManifest.xml。这告诉系统您将自己处理哪些配置更改 - 在这种情况下,什么都不做。

android:configChanges="keyboardHidden|orientation|screenSize"

【讨论】:

  • 妈妈在 中的 mainfest 哪里??
猜你喜欢
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 2017-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
相关资源
最近更新 更多