Android GoogleマップAPI 2を使用して現在の場所を表示しようとしています.wifiが有効な電話でこれを実行します.
ただし、常に経度と緯度 0.0 、 0.0 が表示されます。
これが私のコードです
package com.google.map;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.map.R;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.FragmentManager;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener{
private GoogleMap googlemap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(isGooglePlay())
{
setContentView(R.layout.activity_main);
setUpMap();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId() == R.id.action_legalnotices)
{
startActivity(new Intent(this, LegalNoticesActivity.class));
}
return super.onOptionsItemSelected(item);
}
private boolean isGooglePlay()
{
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if(status == ConnectionResult.SUCCESS)
{
return true;
}
else
{
((Dialog)GooglePlayServicesUtil.getErrorDialog(status, this, 10)).show();
Toast.makeText(getApplicationContext(), "Google play is not available.", Toast.LENGTH_LONG).show();
}
return false;
}
@SuppressLint("NewApi") private void setUpMap()
{
if(googlemap == null)
{
googlemap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
if(googlemap != null)
{
// map code
googlemap.setMyLocationEnabled(true);
googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
LocationManager lo = (LocationManager) getSystemService(LOCATION_SERVICE);
String provider = lo.getBestProvider(new Criteria(),true);
if(provider == null)
{
onProviderDisabled(provider);
}
else
{
onProviderEnabled(provider);
}
Location loc = lo.getLastKnownLocation(provider);
if(loc != null)
{
onLocationChanged(loc);
}
googlemap.setOnMapLongClickListener(onLongClickMapSettings());
}
}
}
private OnMapLongClickListener onLongClickMapSettings() {
// TODO Auto-generated method stub
return new OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng arg0) {
// TODO Auto-generated method stub
Log.i(arg0.toString(),"Long Click");
}
};
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
LatLng latlng = new LatLng(location.getLatitude(),location.getLongitude());
googlemap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
googlemap.moveCamera(CameraUpdateFactory.zoomTo(5));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Location lo = new Location(provider);
LatLng latlng = new LatLng(lo.getLatitude(),lo.getLongitude());
Log.i(latlng.toString(),"Current Location" );
googlemap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
googlemap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 5));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
これが私のAndroidアプリケーションのLog cat値です。
05-07 22:16:31.636: I/lat/lng: (0.0,0.0)(17967): Current Location