-1

所有我有用于查找当前位置的简单 android 应用程序,我尝试了一些代码如下,但它不起作用。它只显示我的静态标记,它不显示我的当前位置标记,所以请建议我:

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


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

</RelativeLayout>

main.java

package com.example.mapannotations;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;

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

public class MainActivity extends FragmentActivity implements LocationListener {
    static final LatLng PointA = new LatLng(23.063905, 72.549629);
    static final LatLng PointB = new LatLng(23.043652, 72.528048);
    static final LatLng PointC = new LatLng(23.008738, 72.561865);
    static final LatLng PointD = new LatLng(23.007158, 72.493887);
    static final LatLng PointE = new LatLng(22.961644, 72.427111);
    private GoogleMap map;
    protected LocationListener locationListener;
    protected Context context;
    protected String latitude, longitude;
    protected boolean gps_enabled, network_enabled;

    public LocationManager loc;

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

        // FOR CURRENT LOCATION....
        loc = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        loc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        // MAP MARKERS
        Marker sha = map.addMarker(new MarkerOptions().position(PointA)
                .title("ShastriNagar BRTS").snippet("BRTS Stop"));
        Marker nar = map.addMarker(new MarkerOptions().position(PointB)
                .title("Laldarwaja").snippet("Laldarwaja AMTS Bus Depot"));
        Marker pal = map.addMarker(new MarkerOptions().position(PointC)
                .title("Paldi").snippet("Paldi Bus Stop"));
        Marker Kar = map.addMarker(new MarkerOptions().position(PointD)
                .title("Karnavati Club")
                .snippet("Karnavati club- Enjoyment centre"));
        Marker jiv = map.addMarker(new MarkerOptions().position(PointE).title(
                "Jirvaraj Park"));

        map.moveCamera(CameraUpdateFactory.newLatLngZoom(PointA, 12));
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        // current Location marker

        Marker cur = map.addMarker(new MarkerOptions().position(
                new LatLng(location.getLatitude(), location.getLongitude()))
                .title("My current Location"));

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Log.d("Latitude", "disable");

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Log.d("Latitude", "enable");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
        Log.d("Latitude", "status");
    }

}

清单.xml

 <uses-permission android:name="com.example.mapannotations.permission.MAPS_RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <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" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

请帮助我...谢谢

4

3 回答 3

3

如果我没记错的话,gps 可能需要一段时间才能启动。你可以试试

String mBestProvider = loc.getBestProvider(new Criteria(), true);
loc.requestLocationUpdates(mBestProvider, 0, 0, this);
于 2013-10-23T07:31:28.433 回答
1

如果您使用适用于 Android 的 Google Maps v2 和 Location API,则您将获取位置更新的旧方式与必须实施的新方式混合在一起。

我的建议是遵循新位置 API 的官方教程:https ://developer.android.com/training/location/receive-location-updates.html

如您所见,您不必使用LocationManager实例来请求更新,但是LocationRequest.

于 2013-10-23T07:35:42.337 回答
0

您的代码没有任何问题。GPS在这里很棘手。如果您已将其设置为查找您的位置,则必须确保您的 GPS 已打开。这意味着它可能无法在您的模拟器上运行。此外,即使 GPS 已打开,它仍然很棘手,因为有时 GPS 需要很长时间才能找到您的位置。

于 2013-10-23T07:51:07.247 回答