Google Maps Android API v2和当前位置

接待台:

我正在尝试使用新的Google Maps Android API V2,并且正在为Android 2.3.3或更高版本开发应用程序。这是一个非常简单的应用程序:它获取用户当前位置(使用gps或网络信号),并使用方向api从数据库获取POI,然后将用户驱动到POI。

我的问题是获取用户当前位置。感谢这篇文章如何在Google Maps Android API v2中获取当前位置?我了解到我无法使用新的Google API更新当前位置。另一个问题是我可以使用来设置自己的位置,GoogleMap setMyLocationEnabled(boolean enabled)但是却无法getMyLocation()知道用户在哪里。

我使用了本指南http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation来获取我的位置,并且我尝试将其集成到Activity中以绘制用户位置。

这是我的代码:

表现

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

    <uses-permission android:name="it.mappe.permission.MAPS_RECEIVE" />

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

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

         <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="MYGMAPSKEY" />
        <activity
            android:name="it.mappe.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>
    </application>

</manifest>

主要活动

package it.mappe;

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;

import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements LocationListener {
    private GoogleMap map;
    private static final LatLng ROMA = new LatLng(42.093230818037,11.7971813678741);
    private LocationManager locationManager;
    private String provider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
        boolean enabledGPS = service
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean enabledWiFi = service
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        // Check if enabled and if not send user to the GSP settings
        // Better solution would be to display a dialog and suggesting to 
        // go to the settings
        if (!enabledGPS) {
            Toast.makeText(this, "GPS signal not found", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fields
        if (location != null) {
            Toast.makeText(this, "Selected Provider " + provider,
                    Toast.LENGTH_SHORT).show();
            onLocationChanged(location);
        } else {

            //do something
        }

    }

    /* Request updates at startup */
    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
    }

    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        double lat =  location.getLatitude();
        double lng = location.getLongitude();
        Toast.makeText(this, "Location " + lat+","+lng,
                Toast.LENGTH_LONG).show();
        LatLng coordinate = new LatLng(lat, lng);
        Toast.makeText(this, "Location " + coordinate.latitude+","+coordinate.longitude,
                Toast.LENGTH_LONG).show();
        Marker startPerc = map.addMarker(new MarkerOptions()
        .position(coordinate)
        .title("Start")
        .snippet("Inizio del percorso")
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
    }


    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

    }


    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();

    }


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

    }

}

主要布局

<?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"
    class="com.google.android.gms.maps.SupportMapFragment" />

它有效,但效果不佳,如您在图片传送时间中所见,它有一个新位置,它添加了一个新标记(我认为是Overlay,我从不使用旧的Google Maps API)。 在此处输入图片说明

如何只显示一个标记?

另外,在我的地图上,我还需要显示POI的标记,因此我认为将所有标记重新绘制在地图上并不是一个好的解决方案。还有另一种获取用户当前位置的最佳方法,每当仅显示一个自定义标记时就更新它!

阿德里安·罗德里格斯(AdriánRodríguez):

您可以呼叫startPerc.remove();仅删除该标记。

这里

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android Google Maps Api v2当前位置为空

适用于Android的Google Maps API v2:标记和折线位置不匹配

Android 2.3和Google Maps API v2

Google Maps Android API v2

在Google Maps Android API v2 DEBUG和RELEASE API密钥之间切换

Android Google Maps API v2:获取我的方位

Google Maps Android API v2授权失败

Android Google Maps API v2检查土地或水域

Android Studio集成了Google Maps API v2

Google Maps API v2 Android中的动态缩放

Google Maps Android API V2崩溃

无法连接到Google Maps Api Android V2

在Google Maps Android API V2上叠加图片

仅运行Google Maps Android API v2 3

Google Maps Android API V2检查是否禁用了Google Maps应用

Android Google Maps v2未显示指南针和位置图标

如何在Google Maps Android API v2中针对两个以上的位置进行CameraUpdate?

使用API V2和mapfragment的google maps

在Google Maps API v2 Android中删除和重新添加单个现有标记的巧妙方法

空白的Android Google Maps v2

Android Google Maps v2测试

Android Google Maps v2导航

使用Google Map API v2在android中绘制从当前位置到目的地位置的路线

使用Google Maps API在Android中查找设备当前位置的纬度和经度值

确保已启用“ Google Maps Android API v2”。尝试实施Google Maps时出现此错误

Google android maps api v2始终显示标记标题

Android Google Maps API V2中心标记

使用Google Maps Android API v2在两点之间绘制路径

Google Maps Android Api V2行驶路线不起作用