我正在尝试获取谷歌地图的边界坐标。我正在使用谷歌地图v2。
我写了以下代码:
try {
Log.e(TAG, "Before init");
initilizeMap();
LatLngBounds curScreen = gmap.getProjection()
.getVisibleRegion().latLngBounds;
System.out.println(curScreen.toString());
//top-left corner
double topleftlatitude=curScreen.northeast.latitude;
double topleftlongitude=curScreen.southwest.longitude;
System.out.println("top left==>"+topleftlatitude+"" +topleftlongitude);
//bottom-right corner
double bottomrightlatitude=curScreen.southwest.latitude;
double bottomrightlongitude=curScreen.northeast.longitude;
System.out.println("bottom right==>"+bottomrightlatitude+"" +bottomrightlongitude);
} catch (Exception e) {
e.printStackTrace();
}
加载地图的功能:
private void initilizeMap() {
System.out.println("On map init");
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager
.findFragmentById(R.id.customclusteredmap);
gmap = mapFragment.getMap();
if (gmap != null)
{
gmap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gmap.getUiSettings().setRotateGesturesEnabled(false);
gmap.setMyLocationEnabled(true);
}
}
我得到的回应是:
07-16 11:26:58.438: I/System.out(9983): LatLngBounds{southwest=lat/lng: (0.0,0.0), northeast=lat/lng: (0.0,0.0)}
07-16 11:26:58.438: I/System.out(9983): top left==>0.00.0
07-16 11:26:58.438: I/System.out(9983): bottom right==>0.00.0
我的地图显示完美,因此我不认为AndroidManifest.xml
中存在任何问题,我仍然使用的权限是:
android:name="com.webguru.wearehere.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.webguru.wearehere.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
正如你所看到的,LatLng界限总是返回0.0。我做错了什么?或者我错过了什么?
解
问题是,设备的gps已关闭。因此,设备无法获取当前位置并返回0.0。
答案 0 :(得分:2)
这是一个时间问题,因为在完全绘制地图之前会调用您的代码。
最干净的解决方案是使用GoogleMap.OnMapLoadedCallback界面,并在onMapLoaded()
回调中运行您的代码。
来自文档:
地图完成渲染后的回调界面。这个 在获取渲染地图所需的所有图块之后发生, 并且所有标签都已完成。
这样您就不会依赖某些硬编码时序,如果地图需要比预期更长的时间加载,这可能会失败。
示例:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, OnMapLoadedCallback {
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
setUpMap();
}
public void setUpMap(){
//set this as map loaded callback
googleMap.setOnMapLoadedCallback(this);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
}
@Override
public void onMapLoaded() {
try {
Log.e("map", "Before init");
//initilizeMap();
LatLngBounds curScreen = googleMap.getProjection()
.getVisibleRegion().latLngBounds;
System.out.println(curScreen.toString());
//top-left corner
double topleftlatitude=curScreen.northeast.latitude;
double topleftlongitude=curScreen.southwest.longitude;
System.out.println("top left==>"+topleftlatitude+" " +topleftlongitude);
//bottom-right corner
double bottomrightlatitude=curScreen.southwest.latitude;
double bottomrightlongitude=curScreen.northeast.longitude;
System.out.println("bottom right==>"+bottomrightlatitude+" " +bottomrightlongitude);
} catch (Exception e) {
e.printStackTrace();
}
}
}
布局xml中的SupportMapFragment:
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
首次发布时的输出:
E/map﹕ Before init
I/System.out﹕ LatLngBounds{southwest=lat/lng: (-72.76405002018156,-63.281260058283806), northeast=lat/lng: (72.76406998847307,63.28122653067112)}
I/System.out﹕ top left==>72.76406998847307 -63.281260058283806
I/System.out﹕ bottom right==>-72.76405002018156 63.28122653067112