【问题标题】:how to display google map with out having internet connection in android如何在没有互联网连接的情况下在android中显示谷歌地图
【发布时间】:2015-08-25 15:23:03
【问题描述】:

如果没有互联网连接,我的谷歌地图不会显示。谷歌地图没有加载。但是获取当前位置的纬度和经度。如何在没有互联网连接的情况下获取谷歌地图。我尝试了下面的代码。 '公共类 MainActivity 扩展 FragmentActivity 实现 LocationListener {

private static final int GPS_ERRORDIALOG_REQUEST = 0;
GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     if (servicesOK()) {
            Toast.makeText(this, "Ready to map!!", Toast.LENGTH_LONG).show();
            setContentView(R.layout.activity_main);

        } else {
             Toast.makeText(this, "Not Ready to show map!!", Toast.LENGTH_LONG).show();

        }


    // Getting reference to the SupportMapFragment of activity_main.xml
    SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting GoogleMap object from the fragment
    googleMap = fm.getMap();

    // Enabling MyLocation Layer of Google Map
    googleMap.setMyLocationEnabled(true);               


     // Getting LocationManager object from System Service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    // Creating a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Getting the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);

    // Getting Current Location
    Location location = locationManager.getLastKnownLocation(provider);

    if(location!=null){
            onLocationChanged(location);
    }

    locationManager.requestLocationUpdates(provider, 20000, 0, this);

}
public boolean servicesOK() {

    int isAvailable = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);

    if (isAvailable == ConnectionResult.SUCCESS) {

        return true;

    } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {

        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
                this, GPS_ERRORDIALOG_REQUEST);
        dialog.show();

    } else {

        Toast.makeText(this, "Cant connect!!", Toast.LENGTH_SHORT).show();

    }
    return false;
}

@Override
public void onLocationChanged(Location location) {

    TextView tvLocation = (TextView) findViewById(R.id.tv_location);

    // Getting latitude of the current location
    double latitude = location.getLatitude();

    // Getting longitude of the current location
    double longitude = location.getLongitude();     

    // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);

    // Showing the current location in Google Map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    // Setting latitude and longitude in the TextView tv_location
    tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );      

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub      
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub      
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub      
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

【问题讨论】:

    标签: android google-maps google-maps-android-api-2


    【解决方案1】:

    您的手机必须需要互联网连接才能首次加载地图。然后它会很容易地显示出来。

    【讨论】:

    • k 很好。我正在研究谷歌地图,我每 2 秒更新一次纬度和经度。只有在当前位置,我的纬度和对数差异很大。即使我在固定位置,然后也得到这个差异?为什么我们会得到这个差异???你知道吗??我正在使用网络提供商。
    • 我做了一个类似的应用程序,它通过短信获取纬度经度。您尝试将这些纬度和经度保存在字符串中,然后在地图上放置标记。
    • 我将这些纬度和经度保存在 arraylist 中,最后我用它来构造一个根。但如果我也处于恒定位置,问题就会变得不同。
    • 如果您将它们存储在ArrayList 中,请使用它们在列表中的位置获取它们并存储在字符串中。检查这个link
    • 然后你可以使用addPolyline方法在每个标记之间画线,如下面的代码sn-p Polyline line=mMap.addPolyline(new PolylineOptions().add(new LatLng(latitude1,longitude1),new LatLng(latitude2,longitude2),new LatLng(latitude3,longitude3),new LatLng(latitude4,longitude4),new LatLng(latitude5,longitude5),new LatLng(latitude6,longitude6)).width(10).color(Color.BLUE));
    猜你喜欢
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2017-08-25
    • 1970-01-01
    • 2015-05-24
    相关资源
    最近更新 更多