[英]How to start the map activity (google map api) of my android application from a specific location with a highest zoom level
注意 - 解决了
这是在我的地图中设置中心的功能,GPS位置我想要更高精度的水平和缩放级别,我必须做出哪些改变?
package cc.co.ratan.www;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class Collegemap extends MapActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.collegemap);
MapView view =(MapView) findViewById(R.id.themap);
view.setBuiltInZoomControls(true);
final MapController control = view.getController();
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listner = new LocationListener() {
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
control.setCenter(new GeoPoint((int)arg0.getLatitude(), (int)arg0.getLongitude()));
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listner);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
这是我编辑后的代码。 (查看并从您的博客进行更改)
package cc.co.ratan.www;
import java.text.DecimalFormat;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
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;
public class Collegemap extends MapActivity implements LocationListener{
private String provider;
GeoPoint myLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//String provider;
setContentView(R.layout.collegemap);
MapView mview =(MapView) findViewById(R.id.themap);
mview.setBuiltInZoomControls(true);
Criteria criteria = new Criteria();
final MapController control = mview.getController();
LocationManager manager = (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
Location location = manager.getLastKnownLocation(manager.NETWORK_PROVIDER);
if (location != null)
plotLocation(location);
else
manager.requestLocationUpdates(
manager.NETWORK_PROVIDER, 500L, 250.0f, (LocationListener) this);
provider = manager.getBestProvider(criteria, false);
LocationListener listner = new LocationListener() {
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(Collegemap.this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(Collegemap.this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
control.setCenter(new GeoPoint((int)arg0.getLatitude(), (int)arg0.getLongitude()));
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listner);
;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
double roundTwoDecimals(double d){
DecimalFormat twoDForm = new DecimalFormat("#.######");
return Double.valueOf(twoDForm.format(d));
}
public void plotLocation(Location location) {
GeoPoint point = new GeoPoint(
(int) (roundTwoDecimals(location.getLatitude()) * 1E6),
(int) (roundTwoDecimals(location.getLongitude()) * 1E6));
myLocation = point;
MapView mview =(MapView) findViewById(R.id.themap);
mview.getController().animateTo(point);
mview.getController().setCenter(point);
zoomToMyLocation();}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
private void zoomToMyLocation() {
if (myLocation != null) {
MapView mview =(MapView) findViewById(R.id.themap);
mview.getController().setZoom(18);
mview.getController().animateTo(myLocation);
}
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}}
@Ratan - 我认为你的活动中缺少一些代码...检查下面的代码plotmylocation会将地图中心映射到gps位置和缩放位置将缩放地图到那个位置你可以在那里添加任何缩放级别而不是18但我认为20将是最大值。
public class MapDragActivity extends MapActivity implements LocationListener{
String pinadd="";
private MapView map=null;
private LocationManager locationManager;
GeoPoint myLocation;
/** Called when the activity is first created. */
@SuppressWarnings("static-access")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maplayout);
map=(MapView)findViewById(R.id.map);
map.setBuiltInZoomControls(true);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
plotLocation(location);
else
locationManager.requestLocationUpdates(
locationManager.NETWORK_PROVIDER, 500L, 250.0f, this);
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null)
plotLocation(location);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@SuppressWarnings("static-access")
@Override
public void onResume() {
super.onResume();
locationManager.requestLocationUpdates(
locationManager.NETWORK_PROVIDER, 1000L, 500.0f, this);
}
@Override
public void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onDestroy(){
locationManager.removeUpdates(this);
super.onDestroy();
}
public void plotLocation(Location location) {
GeoPoint point = new GeoPoint(
(int) (roundTwoDecimals(location.getLatitude()) * 1E6),
(int) (roundTwoDecimals(location.getLongitude()) * 1E6));
myLocation = point;
map.getController().animateTo(point);
map.getController().setCenter(point);
zoomToMyLocation();
}
private void zoomToMyLocation() {
if (myLocation != null) {
map.getController().setZoom(18);
map.getController().animateTo(myLocation);
}
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.