If I recall correctly, the gps may take a while to start. You could try
String mBestProvider = loc.getBestProvider(new Criteria(), true);
loc.requestLocationUpdates(mBestProvider, 0, 0, this);
Вопрос
all i have simple android application for finding current location ,I have tried some code a sbelow,But its not working.It only shows my static markers only,It doesn't show my vurrent Location marker,So please suggest me for it:
Main.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");
}
}
Manifest.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" />
Please help me...thank you
Решение
If I recall correctly, the gps may take a while to start. You could try
String mBestProvider = loc.getBestProvider(new Criteria(), true);
loc.requestLocationUpdates(mBestProvider, 0, 0, this);
Другие советы
You are mixing the old way of getting location updates with the new way you have to implement if you use Google Maps v2 for Android and the Location API.
My suggestion is to follow this official tutorial of the new Location API: https://developer.android.com/training/location/receive-location-updates.html
As you can see, you don't have to use a LocationManager
instance to ask for updates, but LocationRequest
.
There is nothing wrong with your code. The GPS is tricky here. If you've set it to find your location, you have to make sure that your GPS is on. That means that it probably wont work on your emulator. Also, even if the GPS is on, it is still tricky because sometimes, it takes a lot of time for the GPS to find your location.