我是Android的新手,我正在努力使这个应用程序需要我当前位置的位置坐标并在地图上显示我的位置。应用程序的所有其他部分运行得非常好,直到我尝试从fusedLocationApi获取经度和纬度。我已经按照本教程Get the Last Known Location进行了尝试,并尝试在Google Map Activity上实现它。
这是我的java文件:
package com.example.deep.app_project;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
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.MarkerOptions;
import com.google.android.gms.plus.Plus;
public class Map extends FragmentActivity implements GoogleMap.OnMyLocationChangeListener,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
LocationManager lm;
TextView lt, ln;
String provider;
Location l;
double lon= 0, lat =0;
GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
setUpMapIfNeeded();
client = new GoogleApiClient.Builder(this).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
client.connect();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
mMap.setMyLocationEnabled(true);
}
public void button_Map_Back(View v){
Intent MainActivityIntent = new Intent(Map.this, MainActivity.class);
startActivity(MainActivityIntent);
finish();
}
@Override
public void onConnected(Bundle bundle) {
l = LocationServices.FusedLocationApi.getLastLocation(client);
if (l != null) {
lt.setText(String.valueOf(l.getLatitude()));
ln.setText(String.valueOf(l.getLongitude()));
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onMyLocationChange(Location location) {
lt.setText(String.valueOf(location.getLatitude()));
ln.setText(String.valueOf(location.getLongitude()));
}
这是我的.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deep.app_project" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Distance_timer"
android:label="@string/title_activity_distance_timer" >
</activity>
<activity
android:name=".Journey_type"
android:label="@string/title_activity_journey_type" >
</activity>
<activity
android:name=".prev_stat_fragment"
android:label="@string/title_activity_prev_stat_fragment" >
</activity>
<activity
android:name=".Result"
android:label="@string/title_activity_result" >
</activity>
<activity
android:name=".settings_fragment"
android:label="@string/title_activity_settings_fragment" >
</activity>
<activity
android:name=".SimpleFragmentActivity"
android:label="@string/title_activity_simple_fragment" >
</activity>
<activity
android:name=".Time_timer"
android:label="@string/title_activity_time_timer" >
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyD-LoJLRIyTF2mI5HdKULNk1QhBAQlhkRQ"/>
<activity
android:name=".Map"
android:label="@string/title_activity_map" >
</activity>
</application>
答案 0 :(得分:0)
我最近这样做了......并且发现处理不同的文档会让人感到困惑。
我发现使用OnMyLocationChangeListener更容易:
Activity implements GoogleMap.OnMyLocationChangeListener
然后覆盖onLocationChanged:
@Override
public void onMyLocationChange(Location location) {
LatLng pos = new LatLng(location.getLatitude(),location.getLongitude());
CameraUpdate mLoc = CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(pos).zoom(ZOOM_LEVEL).build());
map.moveCamera(mLoc);
mLocMarker = map.addMarker(new MarkerOptions()
.title(TITLE)
.snippet(SNIPPET)
.position(pos)
.draggable(true));
mLocMarker.showInfoWindow();
map.setOnMyLocationChangeListener(null);
}