【问题标题】:Showing current location on android Google Map v2, But app crashes在 android Google Map v2 上显示当前位置,但应用程序崩溃
【发布时间】:2014-09-29 11:25:57
【问题描述】:

这是我的活动代码

package com.example1.heartconnect;

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


public class Maps extends FragmentActivity {

GoogleMap map;

@Override
protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.maps);

    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.gmap)).getMap();
    map.setMyLocationEnabled(true);
    map.setMapType(map.MAP_TYPE_NORMAL);

    Location loc = map.getMyLocation();

    double lat = loc.getLatitude();
    double lon = loc.getLongitude();
    LatLng position = new LatLng(lat, lon);

    Marker marker = map.addMarker(new MarkerOptions()
    .title("Your Location")
    .icon(BitmapDescriptorFactory
         .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
    .position(position));

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 10));


}
}

这是 XML 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<fragment
      android:id="@+id/gmap"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>

这是清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example1.heartconnect"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<permission
    android:name="com.example1.heartconnect.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example1.heartconnect.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/heart_connect"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name="com.example1.heartconnect.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>
    <activity android:name="MyCircle"></activity>
    <activity android:name="Validate"></activity>
    <activity android:name="DisplayContacts"></activity>
    <activity android:name="Maps"></activity>
    <receiver android:name=".SMSBroadcastReceiver">
        <intent-filter>
            <action android:name="android.provider.telephony.SMS_RECEIVED"></action>
        </intent-filter>
    </receiver>
    <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="AIzaSyDUFZIRGsVVpvJxHvjjoUV4G44-43JTnEw"/>  
</application>

</manifest>

当我删除这两条线并手动提供纬度和经度时,该应用程序可以正常工作,并且我可以在该 LatLng 上看到带有标记的裸露的 Google 地图

double lat = loc.getLatitude();
double lon = loc.getLongitude();

我也尝试通过以下方式找到当前位置:

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);

但应用程序也会在此处崩溃。请任何人都可以建议我进行所需的修改,以便我可以在地图上看到我当前的位置... Thnxx 提前:)

【问题讨论】:

    标签: android google-maps currentlocation


    【解决方案1】:

    这是在地图上获取当前位置的代码。

    public class ShowMap extends FragmentActivity implements LocationListener{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_show_location_map);
    
            getMap();
    
      }
      public void getMap(){
    
          int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
    
            // Showing status
            if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
    
                int requestCode = 10;
                Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
                dialog.show();
    
            }else { // Google Play Services are available
    
                // Getting reference to the SupportMapFragment of activity_main.xml
                SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
                // Getting GoogleMap object from the fragment
                googleMap = fm.getMap();
    
                // Enabling MyLocation Layer of Google Map
               googleMap.setMyLocationEnabled(true);
            }
        }
    
    
       @Override
       public void onLocationChanged(Location location) {
       // Getting latitude of the current location
       double latitude = location.getLatitude();
    
       // Getting longitude of the current location
       double longitude = location.getLongitude();
    
       // Creating a LatLng object for the current location
       LatLng latLng = new LatLng(latitude, longitude);
    
       // Showing the current location in Google Map
       googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    
       // Zoom in the Google Map
       googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    
    
       }
    
       @Override
       public void onProviderDisabled(String arg0) {
          // TODO Auto-generated method stub
    
       }
    
       @Override
       public void onProviderEnabled(String arg0) {
           // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
           // TODO Auto-generated method stub
    
      }
    

    你需要import google play store into your project as a library。 并将支持 v4 添加到您的项目中 例如右键单击您的 project -&gt; select properties -&gt;java build path -&gt; select Libraries Tab -&gt; click Add External JARs, and give path of your support v4 jar

    我相信这一步会对你有所帮助。

    【讨论】:

    • 感谢您的回复。我完全按照您的说法做了,但是当我运行我的代码时,我只能看到谷歌地图,它并没有把我带到我的位置。而且由于代码是用 onLocationChanged 方法编写的,有没有其他方法可以只显示当前位置??
    • 嘿!导入 android support v4 库后问题解决了...再次感谢
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 2018-03-14
      • 2013-10-03
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多