简体   繁体   English

谷歌地图Android api v2和当前位置

[英]Google Maps Android api v2 and current location

I'm trying to use the new google maps android api v2 and i'm developing an app for android 2.3.3 or higher. 我正在尝试使用新的谷歌地图android api v2,我正在为Android 2.3.3或更高版本开发一个应用程序。 It's a very simple app: it takes the user current location (using gps or networks signal) it gets from db a POI using direction api, it drives the user to the POI. 这是一个非常简单的应用程序:它使用方向api从db获取POI的用户当前位置(使用gps或网络信号),它将用户驱动到POI。

My problem is get the user current location. 我的问题是获取用户当前位置。 thanks to this post How to get the current location in Google Maps Android API v2? 感谢这篇文章如何获取Google Maps Android API v2中的当前位置? I learned that i can't update current position using new google api. 我了解到我无法使用新的谷歌API来更新当前位置。 Another problem is that I can set my position using GoogleMap setMyLocationEnabled(boolean enabled) but I can't use getMyLocation() to know where user is. 另一个问题是我可以使用GoogleMap setMyLocationEnabled(boolean enabled)设置我的位置GoogleMap setMyLocationEnabled(boolean enabled)但我不能使用getMyLocation()来知道用户的位置。

I used this guide http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation to get my location, and i tried to integrate inside my Activity to draw user position. 我使用本指南http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation获取我的位置,并尝试在我的Activity中集成以绘制用户位置。

here my code: 我的代码:

Manifest 表现

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

main Activity 主要活动

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

    }

}

main layout 主要布局

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

It works but not so good, as you can see in the pictures eveytime there is a new location it adds a new Marker (ex Overlay I think... I never use old google maps api). 它工作但不太好,你可以在图片中看到eveytime有一个新的位置它添加了一个新的标记(ex Overlay我认为......我从不使用旧的谷歌地图api)。 在此输入图像描述

How can I show only one markers? 我怎样才能只显示一个标记?

In addition on my map i need to display also POI's marker, so i think that it's not a good solution delate all marker and redraw it on the maps. 另外在我的地图上我还需要显示POI的标记,所以我认为这不是一个好的解决方案,所有标记都要降低并在地图上重绘。 There is another best way to get user current location, updating it every moment displaying just one custom marker?! 还有另一种获取用户当前位置的最佳方法,每次只显示一个自定义标记时更新它?!

You can call startPerc.remove(); 你可以调用startPerc.remove(); to delete only this marker. 仅删除此标记。

From here 这里开始

为什么不使用startPerc.setPosition来移动标记而不是继续添加和删除...

通过此方法获取其他位置时设置标记的位置

marker. set position (location)

you should use googlemap.clear() before adding the marker because everytime when location changed it add new marker on map with oldest marker. 您应该在添加标记之前使用googlemap.clear() ,因为每次更改位置时,都会在地图上添加最新标记的新标记。 so use googlemap.clear() before add the marker in location changed. 所以在更改位置之前添加标记之前使用googlemap.clear()。 it will remove oldest marker and add new marker. 它将删除最旧的标记并添加新标记。 googlemap is the instance of GoogleMap Class. googlemap是GoogleMap Class的实例。

You could replace the marker with a new one: 您可以用新的标记替换标记:

if(userMarker!=null) userMarker.remove();
    userMarker = theMap.addMarker(new MarkerOptions()
    .position(lastLatLng)
    .title("You are here")
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.your_loc_icon))
    .snippet("Your current location"));

Full shortcut for defining the Google Map android v2 in android...... 在android中定义Google Map android v2的完整快捷方式......

XML File : XML文件:

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

Menifest file : 清单档案:

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Map permission Starts -->
<permission
    android:name="com.example.mapdemoapiv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.mapdemoapiv2.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<!-- Map permission Starts -->

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.mapdemoapiv2.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=".MapDetail" >
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="Your Key" />

    <uses-library android:name="com.google.android.maps" />
</application>

Activity class : 活动类:

public class MainActivity extends android.support.v4.app.FragmentActivity
{
   GoogleMap googleMap;
    MarkerOptions markerOptions;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setUpMapIfNeeded();

    GetCurrentLocation();

    }

private void setUpMapIfNeeded() {
    if (googleMap == null) {

        Log.e("", "Into null map");
        googleMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

        googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(
                MainActivity.this));

        if (googleMap != null) {
            Log.e("", "Into full map");
            googleMap.setMapType(googleMap.MAP_TYPE_NORMAL);
            googleMap.getUiSettings().setZoomControlsEnabled(false);
        }
    }
}

private void GetCurrentLocation() {

    double[] d = getlocation();
    Share.lat = d[0];
    Share.lng = d[1];

     googleMap
            .addMarker(new MarkerOptions()
                    .position(new LatLng(Share.lat, Share.lng))
                    .title("Current Location")
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.dot_blue)));

             googleMap
                .animateCamera(CameraUpdateFactory.newLatLngZoom(
                        new LatLng(Share.lat, Share.lng), 5));
}

public double[] getlocation() {
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = lm.getProviders(true);

    Location l = null;
    for (int i = 0; i < providers.size(); i++) {
        l = lm.getLastKnownLocation(providers.get(i));
        if (l != null)
            break;
    }
    double[] gps = new double[2];

    if (l != null) {
        gps[0] = l.getLatitude();
        gps[1] = l.getLongitude();
    }
    return gps;
}

First of all in map api v2 use LocationClient to retreive your location, see: http://developer.android.com/training/location/retrieve-current.html 首先在map api v2中使用LocationClient来检索您的位置,请参阅: http//developer.android.com/training/location/retrieve-current.html

Iportant: to kick start API, call this in onStart of main Activity: Iportant:启动API,在主Activity的onStart中调用它:

private void kickStardedLocationManager(){

 // Acquire a reference to the system Location Manager
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

 // Define a listener that responds to location updates
    android.location.LocationListener locationListener = new android.location.LocationListener() {
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
            //MainActivity.this.makeUseOfNewLocation(location);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
      };

      long l = 10;
      float f = 100;
   // Register the listener with the Location Manager to receive location updates
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); //LocationManager.GPS_PROVIDER
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM