谷歌地图Android的API V2和当前位置 [英] Google Maps Android api v2 and current location

查看:117
本文介绍了谷歌地图Android的API V2和当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用新的谷歌地图的Andr​​oid API v2和我正在开发适用于Android 2.3.3或更高版本的应用程序。 这是一个非常简单的应用程序: 它需要用户的当前位置(使用GPS或网络信号) 从dB的POI得到 使用方向的api,它驱动用户到POI

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

我的问题是获取用户当前位置。感谢这个帖子<一个href="http://stackoverflow.com/questions/13756261/how-to-get-the-current-lcoation-in-google-maps-android-api-v2">How获取当前lcoation在谷歌地图Android的API V2?我才知道,我可以不使用新的谷歌API更新当前位置。 另一个问题是,我可以使用 GoogleMap的setMyLocationEnabled(布尔启用)设置我的位置,但我不能使用 getMyLocation()要知道用户。

My problem is get the user current location. thanks to this post How to get the current lcoation in Google Maps Android API v2? I learned that i can't update current position using new google 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.

我用这个指南<一href="http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation">http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation让我的位置,我想我的活动里面集成吸取用户的位置。

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.

我在这里的code:

清单

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

它的工作原理,但不是那么好,你可以在图片中看到eveytime有一个新的位置,它增加了一个新的标志物(如覆盖我想......我从来没有使用旧的谷歌地图API)。

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).

我怎么能只显示一个标志?

How can I show only one markers?

另外我的地图我还需要显示POI的标记,所以我认为这不是一个好的解决办法啦免费统计所有标记,重新绘制在地图上。 还有就是要获取用户当前位置的另一种最好的方式,每一个时刻只显示一个自定义标记更新呢?!

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. There is another best way to get user current location, updating it every moment displaying just one custom marker?!

推荐答案

您可以致电 startPerc.remove(); 只删除该标记。

You can call startPerc.remove(); to delete only this marker.

从<一个href="http://developer.android.com/reference/com/google/android/gms/maps/model/Marker.html#remove%28%29">here

这篇关于谷歌地图Android的API V2和当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆