所有我有用于查找当前位置的简单 android 应用程序,我尝试了一些代码如下,但它不起作用。它只显示我的静态标记,它不显示我的当前位置标记,所以请建议我:
主.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");
}
}
清单.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" />
请帮助我...谢谢