简体   繁体   English

Google Maps API V2标记未正确显示

[英]Google Maps API V2 marker not showing on correct position

I've been following a series of tutorials to make a basic restaurant locator app for android using Google places with maps API v2. 我一直在遵循一系列教程,以使用带有Google Maps API v2的Google地方为Android创建基本的餐厅定位器应用。 The problem I am having, is that my gps location is correct, but the marker drawn on the map to show the users location is approximately 60-65 miles south east of the users actual location. 我遇到的问题是我的gps位置正确,但是在地图上绘制以显示用户位置的标记在用户实际位置的东南约60-65英里处。

Here is the code for my Location class: 这是我的Location类的代码:

public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location = null; 
double latitude; 
double longitude; 

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } 
        else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network Enabled");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }

    return latitude;
}

public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }

    return longitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}

and finally here is the code for my map activity: 最后是我的地图活动的代码:

public class PlacesMapActivity extends FragmentActivity {
double latitude;
double longitude;
LatLng USER=null;
LatLng LOCATION=null;
PlacesList nearPlaces;
private GoogleMap map;
private int userIcon, locationIcon;
String address;
String name;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_places);

    userIcon = R.drawable.mark_red;
    locationIcon = R.drawable.mark_blue;

    Intent i = getIntent();

    String user_latitude = i.getStringExtra("user_latitude");
    String user_longitude = i.getStringExtra("user_longitude");
    String user_location = (user_latitude + ", " + user_longitude);

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

    USER = new LatLng((int) (Double.parseDouble(user_latitude)),
            (int) (Double.parseDouble(user_longitude)));

    Marker user = map.addMarker(new MarkerOptions().position(USER)
            .title("This is you.")
            .snippet(user_location)
            .icon(BitmapDescriptorFactory
                    .fromResource(userIcon)));

    nearPlaces = (PlacesList) i.getSerializableExtra("near_places");
    if(nearPlaces != null) {
        for(Place place : nearPlaces.results) {             
            latitude = place.geometry.location.lat;
            longitude = place.geometry.location.lng;
            name = place.name;
            address= place.vicinity;
        }

        final LatLngBounds.Builder builder = new LatLngBounds.Builder();

        for(int c = 0; c < nearPlaces.results.size(); c++){
            final LatLng pos = new LatLng(nearPlaces.results.get(c).geometry.location.lat,
                    nearPlaces.results.get(c).geometry.location.lng);
            builder.include(pos);
            map.addMarker(new MarkerOptions().position(pos)
                        .title(nearPlaces.results.get(c).name)
                        .snippet(nearPlaces.results.get(c).vicinity)
                        .icon(BitmapDescriptorFactory
                                .fromResource(locationIcon)));
        }
    }

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(USER, 14));

    map.animateCamera(CameraUpdateFactory.zoomTo(9), 2000, null);

} }

I tried to attach a screenshot, apparently I can't yet. 我尝试附加屏幕截图,显然还不能。 I set the marker snippet to show the users latitude and longitude when clicked. 我设置了标记代码段,以在单击时向用户显示纬度和经度。 When I click the users marker it shows the correct longitude and latitude (checked them on the internet, its within a block of my actual location), but as I stated the marker for the user always shows 60-65 miles south east. 当我单击用户标记时,它会显示正确的经度和纬度(在Internet上对其进行检查,它位于我的实际位置的某个范围内),但是正如我所说,用户的标记始终显示东南60-65英里。 Also, the locations of the Places results are all correct and the markers in the correct locations. 另外,地方信息结果的位置都正确,标记也位于正确的位置。

any help is greatly appreciated. 任何帮助是极大的赞赏。 Cheers. 干杯。

In your code below 在下面的代码中

 USER = new LatLng((int) (Double.parseDouble(user_latitude)),
        (int) (Double.parseDouble(user_longitude)));

Don't convert latitude-longitude to int , instead keep them as double . 不要将经纬度转换为int ,而是将其保留为double

Cheers ! 干杯!

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

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