Моя программа должна иметь возможность:
- Изменить вид карты
- Увеличить текущее местоположение телефона
- Добавить маркер
- Отображение предупреждения о значениях Широта и долгота
Когда я использую GPS_PROVIDER, функции 1-4 работают в моем эмуляторе (потому что я вручную передаю ему значения широты и долготы). Но когда я создал файл apk, функции 2-4 не работают.
Здесь мой класс.
package com.locator.map;
import java.text.DecimalFormat;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class Main extends MapActivity
{
MapController controller;
double currentLat;
double currentLon;
LocationManager manager;
boolean locationChanged = false;
Button retrieveLocationButton, toggleViewButton;
boolean streetview = false;
Drawable d;
List<Overlay> overlaylist;
MapView map;
Criteria criteria;
String bestProvider;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = (MapView) findViewById (R.id.googleMap);
map.setBuiltInZoomControls(true);
map.setSatellite(true);
controller = map.getController();
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
manager = (LocationManager) this.getSystemService (Context.LOCATION_SERVICE);
bestProvider = manager.getBestProvider(criteria, true);
retrieveLocationButton = (Button) findViewById(R.id.retrieveLocation);
toggleViewButton = (Button) findViewById(R.id.changeView);
retrieveLocationButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
showCurrentLocation();
}
});
toggleViewButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (streetview == false)
{
map.setSatellite(false);
map.setStreetView(true);
streetview=true;
}
else if (streetview == true)
{
map.setStreetView(false);
map.setSatellite(true);
streetview=false;
}
}
});
LocationListener listener = new LocationListener()
{
public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
//Retrieve the current GPS coordinates
currentLat = location.getLatitude();
currentLon = location.getLongitude();
locationChanged = true;
}
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Your GPS services are not activated.", Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Your GPS services are now activated.", Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Your GPS service provider is changed.", Toast.LENGTH_LONG).show();
}
};
manager.requestLocationUpdates(bestProvider, 1000, 1, listener); //--> To update current location (coordinates [latitude & longitude]).
}
protected void showCurrentLocation()
{
// TODO Auto-generated method stub
//If location is changed or updated - Zoom in to current location, add a marker and show a dialog with current coordinates.
if (locationChanged == true)
{
//Set the map view and zoom level animation
GeoPoint currentLocation = new GeoPoint ((int) (currentLat * 1E6), (int) (currentLon * 1E6));
controller.setCenter(currentLocation);
controller.setZoom(15);
//Show the current coordinates (Convert to String and round off - 6 decimal places)
String printLat = new DecimalFormat("0.######").format((double)currentLat);
String printLon = new DecimalFormat("0.######").format((double)currentLon);
AlertDialog alert = new AlertDialog.Builder(Main.this).create();
alert.setTitle("Current Location:");
alert.setMessage("Latitude: " + printLat + "\n" + "Longitude: " + printLon);
alert.setButton("Close", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
//Alert Dialog won't work without a listener
//Do nothing (This will simply close your alert dialog)
}
});
alert.show();
//Add a marker
overlaylist = map.getOverlays();
d = getResources().getDrawable(R.drawable.ic_launcher);
CustomPinpoint marker = new CustomPinpoint (d, Main.this);
OverlayItem overlayitem = new OverlayItem (currentLocation, "1st String", "2nd String");
marker.insertPinpoint(overlayitem);
overlaylist.add(marker);
}
}
@Override
protected boolean isRouteDisplayed()
{
// TODO Auto-generated method stub
return false;
}
}
Я также пробовал использовать NETWORK_PROVIDER. Все работает в моем телефоне, но не возвращает точные значения широты и долготы. Он добавляет маркер, но он находится примерно в 1 километре от моего фактического местоположения.
Здесь мой класс использует NETWORK_PROVIDER:
package com.locator.map;
import java.text.DecimalFormat;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class Main extends MapActivity
{
MapController controller;
double currentLat;
double currentLon;
LocationManager manager;
boolean locationChanged = false;
Button retrieveLocationButton, toggleViewButton;
boolean streetview = false;
Drawable d;
List<Overlay> overlaylist;
MapView map;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = (MapView) findViewById (R.id.googleMap);
map.setBuiltInZoomControls(true);
map.setSatellite(true);
controller = map.getController();
manager = (LocationManager) this.getSystemService (Context.LOCATION_SERVICE);
retrieveLocationButton = (Button) findViewById(R.id.retrieveLocation);
toggleViewButton = (Button) findViewById(R.id.changeView);
retrieveLocationButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
showCurrentLocation();
}
});
toggleViewButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (streetview == false)
{
map.setSatellite(false);
map.setStreetView(true);
streetview=true;
}
else if (streetview == true)
{
map.setStreetView(false);
map.setSatellite(true);
streetview=false;
}
}
});
LocationListener listener = new LocationListener()
{
public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
//Retrieve the current GPS coordinates
currentLat = location.getLatitude();
currentLon = location.getLongitude();
locationChanged = true;
}
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Your GPS services are not activated.", Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Your GPS services are now activated.", Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Your GPS service provider is changed.", Toast.LENGTH_LONG).show();
}
};
manager.requestLocationUpdates(NETWORK_PROVIDER, 1000, 1, listener); //--> To update current location (coordinates [latitude & longitude]).
}
protected void showCurrentLocation()
{
// TODO Auto-generated method stub
//If location is changed or updated - Zoom in to current location, add a marker and show a dialog with current coordinates.
if (locationChanged == true)
{
//Set the map view and zoom level animation
GeoPoint currentLocation = new GeoPoint ((int) (currentLat * 1E6), (int) (currentLon * 1E6));
controller.setCenter(currentLocation);
controller.setZoom(15);
//Show the current coordinates (Convert to String and round off - 6 decimal places)
String printLat = new DecimalFormat("0.######").format((double)currentLat);
String printLon = new DecimalFormat("0.######").format((double)currentLon);
AlertDialog alert = new AlertDialog.Builder(Main.this).create();
alert.setTitle("Current Location:");
alert.setMessage("Latitude: " + printLat + "\n" + "Longitude: " + printLon);
alert.setButton("Close", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
//Alert Dialog won't work without a listener
//Do nothing (This will simply close your alert dialog)
}
});
alert.show();
//Add a marker
overlaylist = map.getOverlays();
d = getResources().getDrawable(R.drawable.ic_launcher);
CustomPinpoint marker = new CustomPinpoint (d, Main.this);
OverlayItem overlayitem = new OverlayItem (currentLocation, "1st String", "2nd String");
marker.insertPinpoint(overlayitem);
overlaylist.add(marker);
}
}
@Override
protected boolean isRouteDisplayed()
{
// TODO Auto-generated method stub
return false;
}
}
Вот мой манифест:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.locator.map"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application android:icon="@drawable/ic_launcher" android:label="Locator" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".Main"
android:label="Locator" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Я думаю, GPS работает здесь, в моей области, приложение Google Maps возвращает точное местоположение, когда я его использую. Я попытался использовать свое приложение (как NETWORK_PROVIDER, так и GPS_PROVIDER) в том же месте, где я использовал Карты Google:
- NETWORK_PROVIDER по-прежнему возвращает неточный результат.
- GPS_PROVIDER по-прежнему не работает.
Пожалуйста, помогите мне.