如何开始我的Android应用程序的活动地图(谷歌地图API)从一个特定的位置具有最高缩放级别 [英] How to start the map activity (google map api) of my android application from a specific location with a highest zoom level
问题描述
注意 - 解决
这是它设置中心我的地图,与GPS位置的功能,我想更多的最高precise水平和缩放级别,什么样的变化,我必须做什么?
包cc.co.ratan.www;
进口android.content.Context;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.MapActivity;
进口com.google.android.maps.MapController;
进口com.google.android.maps.MapView;
公共类Collegemap扩展MapActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.collegemap);
图形页面视图=(图形页面)findViewById(R.id.themap);
view.setBuiltInZoomControls(真正的);
最后MapController控制= view.getController();
LocationManager经理=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
LocationListener的听者=新LocationListener的(){
公共无效onStatusChanged(字符串为arg0,INT ARG1,捆绑ARG2){
// TODO自动生成方法存根
}
公共无效onProviderEnabled(字符串为arg0){
// TODO自动生成方法存根
}
公共无效onProviderDisabled(字符串为arg0){
// TODO自动生成方法存根
}
公共无效onLocationChanged(位置为arg0){
// TODO自动生成方法存根
control.setCenter(新的GeoPoint((int)的arg0.getLatitude(),(int)的arg0.getLongitude()));
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,听者);
}
@覆盖
保护的布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
}
这是我的code I编辑后。 (看了看,从您的博客所做的更改)
包cc.co.ratan.www;
进口java.text.DecimalFormat中;
进口android.content.Context;
进口android.location.Criteria;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.widget.Toast;
进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.MapActivity;
进口com.google.android.maps.MapController;
进口com.google.android.maps.MapView;
公共类Collegemap扩展MapActivity实现LocationListener的{
私人字符串提供商;
的GeoPoint myLocation;
@覆盖
保护无效的onCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
//字符串提供商;
的setContentView(R.layout.collegemap);
图形页面MVIEW =(图形页面)findViewById(R.id.themap);
mview.setBuiltInZoomControls(真正的);
标准标准=新标准();
最后MapController控制= mview.getController();
LocationManager经理=(LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
位置位置= manager.getLastKnownLocation(manager.NETWORK_PROVIDER);
如果(位置!= NULL)
plotLocation(位置);
其他
manager.requestLocationUpdates(
manager.NETWORK_PROVIDER,500L,250.0f,(LocationListener的)这一点);
供应商= manager.getBestProvider(标准,FALSE);
LocationListener的听者=新LocationListener的(){
公共无效onStatusChanged(字符串为arg0,INT ARG1,捆绑ARG2){
// TODO自动生成方法存根
}
公共无效onProviderEnabled(字符串为arg0){
// TODO自动生成方法存根
Toast.makeText(Collegemap.this,已启用新的供应商+供应商,
Toast.LENGTH_SHORT).show();
}
公共无效onProviderDisabled(字符串为arg0){
// TODO自动生成方法存根
Toast.makeText(Collegemap.this,残疾人提供+供应商,
Toast.LENGTH_SHORT).show();
}
公共无效onLocationChanged(位置为arg0){
// TODO自动生成方法存根
control.setCenter(新的GeoPoint((int)的arg0.getLatitude(),(int)的arg0.getLongitude()));
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,听者);
;
}
@覆盖
保护的布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
公共无效onLocationChanged(位置定位){
// TODO自动生成方法存根
}
双roundTwoDecimals(双D){
DecimalFormat的twoDForm =新的DecimalFormat(#######);
返回Double.valueOf(twoDForm.format(d)条);
}
公共无效plotLocation(位置定位){
的GeoPoint点=新的GeoPoint(
(中间体)(roundTwoDecimals(location.getLatitude())* 1E6),
(中间体)(roundTwoDecimals(location.getLongitude())* 1E6));
myLocation =点;
图形页面MVIEW =(图形页面)findViewById(R.id.themap);
。mview.getController()animateTo(点);
mview.getController()setCenter(点)。
zoomToMyLocation();}
公共无效onProviderDisabled(字符串提供商){
// TODO自动生成方法存根
}
私人无效zoomToMyLocation(){
如果(myLocation!= NULL){
图形页面MVIEW =(图形页面)findViewById(R.id.themap);
mview.getController()setZoom(18)。
。mview.getController()animateTo(myLocation);
}
}
公共无效onProviderEnabled(字符串提供商){
// TODO自动生成方法存根
}
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
// TODO自动生成方法存根
}}
@Ratan - 我觉得有些code是从你的活动缺少... 检查低于code plotmylocation将围绕地图的GPS位置和放大器; zoomtomylocation将放大地图,以该位置可以有补充,而不是18任何缩放级别,但我认为20将是最大的价值。
公共类MapDragActivity扩展MapActivity实现LocationListener的{
字符串pinadd =;
私人MapView的地图= NULL;
私人LocationManager locationManager;
的GeoPoint myLocation;
/ **第一次创建活动时调用。 * /
@燮pressWarnings(静态访问)
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.maplayout);
地图=(图形页面)findViewById(R.id.map);
map.setBuiltInZoomControls(真正的);
locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
位置位置= locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
如果(位置!= NULL)
plotLocation(位置);
其他
locationManager.requestLocationUpdates(
locationManager.NETWORK_PROVIDER,500L,250.0f,这一点);
}
公共无效onLocationChanged(位置定位){
// TODO自动生成方法存根
如果(位置!= NULL)
plotLocation(位置);
}
公共无效onProviderDisabled(字符串提供商){
}
公共无效onProviderEnabled(字符串提供商){
}
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
}
@燮pressWarnings(静态访问)
@覆盖
公共无效onResume(){
super.onResume();
locationManager.requestLocationUpdates(
locationManager.NETWORK_PROVIDER,1000L,500.0f,这一点);
}
@覆盖
公共无效的onPause(){
super.onPause();
locationManager.removeUpdates(本);
}
@覆盖
公共无效的onDestroy(){
locationManager.removeUpdates(本);
super.onDestroy();
}
公共无效plotLocation(位置定位){
的GeoPoint点=新的GeoPoint(
(中间体)(roundTwoDecimals(location.getLatitude())* 1E6),
(中间体)(roundTwoDecimals(location.getLongitude())* 1E6));
myLocation =点;
。map.getController()animateTo(点);
map.getController()setCenter(点)。
zoomToMyLocation();
}
私人无效zoomToMyLocation(){
如果(myLocation!= NULL){
map.getController()setZoom(18)。
。map.getController()animateTo(myLocation);
}
}
保护的布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
}
Note -- Solved
This is the function which is setting the center in my map , with GPS locations i want more highest precise level and zoom level , what changes i have to make ?
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;
}
}
this is my code after i edited . (looked and made changes from your blog )
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 -- I think some code is missing from your activity... Check below code plotmylocation will center map to gps location & zoomtomylocation will zoom map to that location you can add any zoom level there instead of 18 but I think 20 will be max value.
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;
}
}
这篇关于如何开始我的Android应用程序的活动地图(谷歌地图API)从一个特定的位置具有最高缩放级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!