如何更新用户在谷歌地图中的当前位置

时间:2011-05-17 13:27:08

标签: android google-maps gps

嗨朋友们         我想在Google地图中显示用户的当前位置,并在地图中添加他的位置标记,以及如何在每秒后更新他们在谷歌地图中的位置.. ???

2 个答案:

答案 0 :(得分:0)

myLocationOverlay = new MyLocationOverlay(this, mapView); 
    mapView.getOverlays().add(myLocationOverlay); 
    myLocationOverlay.enableMyLocation(); 
    myLocationOverlay.runOnFirstFix(addfirststandort);

答案 1 :(得分:0)

java文件

public class MYGIRNE extends FragmentActivity {

    GoogleMap map;

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


        SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting Map for the SupportMapFragment
        map = fm.getMap();

        if(map!=null){

            // Enable MyLocation Button in the Map
            map.setMyLocationEnabled(true);
         LocationManager manager=(LocationManager) getSystemService(LOCATION_SERVICE);
            Criteria criteria=new Criteria();
            String provider =manager.getBestProvider(criteria, true);
            Location cloc=manager.getLastKnownLocation(provider);
            double lat=0;
            double lng =0;
             lat =cloc.getLatitude();
             lng= cloc.getLongitude();
            LatLng latlng = new LatLng (lat,lng);
            map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
           map.animateCamera(CameraUpdateFactory.zoomTo(15));
           map.addMarker(new MarkerOptions()
             .title("Your Current Location")
            .position(latlng));
            .icon(BitmapDescriptorFactory.defaultMarker(
                      BitmapDescriptorFactory.HUE_AZURE)));
}
}

xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.firsttry.cyptour.MYGIRNE" >

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

</RelativeLayout>

还在清单文件中包含以下内容

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="enter your APIKEY" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

此代码应该是应用程序的一个元素

然后根据您的清单文件的使用权限包含此内容

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!-- External storage for caching. -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- My Location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- Maps API needs OpenGL ES 2.0. -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

我希望这有帮助