问题
Hi I've an android app that finds your location on google maps, but when I've started the app it started from Africa not in my current city,country,location etc. I've already checked info's on developer.android.com related with location issues but the problem persists.
Here is the code; any ideas? thanks..
package com.kodlab.nerdeyim;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
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;
public class MainActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private LocationClient locationClient;
private LocationRequest locationRequest;
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationClient = new LocationClient(this, this, this);
locationRequest = LocationRequest.create()
.setInterval(5000)
.setFastestInterval(500)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = supportMapFragment.getMap();
googleMap.setMyLocationEnabled(true);
}
@Override
public void onLocationChanged(Location location) {
HaritadaKonumGosterAsyncTask task = new HaritadaKonumGosterAsyncTask();
task.execute(new Location[] {location});
}
@Override
public void onConnected(Bundle connectionHint) {
locationClient.requestLocationUpdates(locationRequest, this);
}
@Override
public void onDisconnected() {}
@Override
public void onConnectionFailed(ConnectionResult result) {}
@Override
protected void onResume() {
super.onResume();
locationClient.connect();
}
@Override
public void onPause() {
super.onPause();
if(locationClient.isConnected())
locationClient.removeLocationUpdates(this);
locationClient.disconnect();
}
private class HaritadaKonumGosterAsyncTask extends AsyncTask<Location, Void, LatLng> {
@Override
protected LatLng doInBackground(Location... params) {
Location konum = params[0];
return new LatLng(konum.getLatitude(), konum.getLongitude());
}
@Override
protected void onPostExecute(LatLng konum) {
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(konum, 15));
}
}
}
回答1:
You can set it in your XML layout also (sets the middlish of North America):
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="pl.mg6.android.maps.extensions.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraBearing="0"
map:cameraTargetLat="53.938305"
map:cameraTargetLng="-112.763672"
map:cameraTilt="30"
map:cameraZoom="2" />
I'm using the android maps extension in that example. Great library BTW. I really like this approach as it loads it right away and you never see Africa. IMO it seems to be the best.
Note! I should warn you that I get a compile error until I make a space and then save the file again when I start Eclipse up after I'm done for the day... Anyone know why this is?
回答2:
Thanks to your code solved my problem, now I help
public void onLocationChanged(Location location)
{
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
float speed = location.getSpeed();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
CameraPosition camPos = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude))
.zoom(18)
.bearing(location.getBearing())
.tilt(70)
.build();
CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos);
googleMap.animateCamera(camUpd3);
}
回答3:
You don't need a AsycTask for this remove it and do everything in onlocationchanged() method itself
public void onLocationChanged(Location location)
{
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location, 15));
}
来源:https://stackoverflow.com/questions/18932325/android-how-to-focus-on-current-position