0

我的谷歌地图应用程序有一些问题。目前,该应用程序仅支持放大我的位置,但无法正常工作。我的位置在地图的顶部结束。这是我的代码:MainActivity:

public class MainActivity extends Activity implements LocationListener {

private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (isGooglePlayOk()) {

        setContentView(R.layout.activity_main);
        setMap();
        googleMap.setMyLocationEnabled(true);


    }

}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.action_legalnotices:
        startActivity(new Intent(this, LegalNoticeActivity.class));
        return true;

    default:
        return false;
    }
}

private boolean isGooglePlayOk() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if (status == ConnectionResult.SUCCESS) {
        return (true);
    }

    else {
        ((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 10))
                .show();

    }
    return (false);

}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setMap() {

    if (googleMap == null) {

        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.main.map)).getMap();
        if (googleMap != null) {

        }

        googleMap.setMyLocationEnabled(true);
        LocationManager la = (LocationManager) getSystemService(LOCATION_SERVICE);

        String provider = la.getBestProvider(new Criteria(), true);
        Location loc = la.getLastKnownLocation(provider);
        if (provider != null) {
            onLocationChanged(loc);

        }

        googleMap.setOnMapLongClickListener(onLongClickMapSettiins());

    }
}

private OnMapLongClickListener onLongClickMapSettiins() {
    // TODO Auto-generated method stub

    return new OnMapLongClickListener() {

        @Override
        public void onMapLongClick(LatLng point) {
            Toast.makeText(getApplicationContext(), "OK",
                    Toast.LENGTH_SHORT).show();

        }
    };
}

@Override
public void onLocationChanged(Location location) {

    LatLng latlong = new LatLng(location.getLatitude(),
            location.getLongitude());

    googleMap.setMyLocationEnabled(true);

    CameraPosition cp = new CameraPosition.Builder().target(latlong)
            .zoom(15).build();

    googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cp));

}

@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

}
 }

活动主:

    <?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+main/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mapateste"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="(My Api is working fine)" />

        <activity
            android:name="com.example.mapateste.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.mapateste.LegalNoticeActivity"
            android:label="@string/title_activity_legal_notice" >
        </activity>
    </application>

我只能在我的设备中测试。地图工作正常,但我无法立即在屏幕中心找到我的位置,只能单击 mylocation 按钮。有人可以帮助我吗?

4

8 回答 8

2

只需声明一个您想要集中的点。

  LatLng cur_Latlng = new LatLng(21.0000, 78.0000);
  gm.moveCamera(CameraUpdateFactory.newLatLng(cur_Latlng));
  gm.animateCamera(CameraUpdateFactory.zoomTo(4));

所需的缩放级别在 2.0 到 21.0 的范围内。

于 2013-09-19T05:12:40.240 回答
1
// try this
@Override
public void onLocationChanged(Location location) {

        LatLng latlong = new LatLng(location.getLatitude(),
                location.getLongitude());

        googleMap.animateCamera(CameraUpdateFactory.newLatLng(latlong));
        googleMap.animateCamera(CameraUpdateFactory.zoomBy(15));

}
于 2013-09-19T04:48:57.363 回答
1

这适用于 Google Map V2 缩放的当前位置

 double lat= location.getLatitude();
 double lng = location.getLongitude();
 LatLng ll = new LatLng(lat, lng);
 googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 20));
于 2014-01-09T06:17:57.280 回答
1

你的 min sdk 是 8。你应该使用SupportMapFragment. 您的课程必须扩展FragmentActivtiy

检查以下链接中开发人员指南标题上方的行

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment

<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"  
android:layout_width="match_parent"
android:layout_height="match_parent"/>

利用SupportMapFragment

SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap(); 

确保您已添加支持库

还要确保您导入了以下内容

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment

缩放

 CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latlong,20);
 googleMap.moveCamera(update);
于 2013-09-19T04:36:38.510 回答
1

请注意,我的位置图层不返回任何数据。如果您希望以编程方式访问位置数据,请使用 Location API。

在您的 MainActivity(onCreate 方法)中

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(MainActivity.this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

在方法 onMapReady

@Override
public void onMapReady(GoogleMap googleMap) {

    GoogleMap mMap = googleMap;
    // Set blue point on the map at your cuurent location
    mMap.setMyLocationEnabled(true);
    // Show zoon controls on the map layer
    mMap.getUiSettings().setZoomControlsEnabled(true);
    //Show My Location Button on the map
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
}

方法 onConnected

@Override
public void onConnected(Bundle bundle) {

    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLastLocation != null) {
        LocationListener.latlonInit= new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude());
        CameraPosition target = CameraPosition.builder().tilt(66.0f).target(LocationListener.latlonInit)
                .zoom(MainActivity.ZOOM).build();
        mMap.moveCamera(CameraUpdateFactory.newCameraPosition(target));
    }
}
于 2015-06-18T14:24:45.283 回答
0

如果 setMap 的条件为真,activity_map.xml 用于显示地图,否则转到您的常规 activity_main.xml。

public class MainActivity extends FragmentActivity {
    GoogleMap mMap;


if (isGooglePlayOk()) {

        setContentView(R.layout.activity_map);

        if (setMap()) {
            mMap.setMyLocationEnabled(true);
        }
        else {
            //your code
        }
    }
    else {
        setContentView(R.layout.activity_main);
    }

setMap 的方法:

private boolean setMap() {
    if (mMap == null) {
        SupportMapFragment mapFrag =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mMap = mapFrag.getMap();
    }
    return (mMap != null);
}
于 2013-09-19T05:45:08.870 回答
0

你的课应该扩展片段活动

public class MainActivity extends FragmentActivity {
//assign any arbitrary value to GPS_ERRODIALOG_REQUEST
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap googlemap;

}

对于 Google Service Ok 方法,我会执行以下操作:

public boolean isGooglePlayOk(){
    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, "Can't connect to Goolge Play", Toast.LENGTH_SHORT).show();
}
return false;

}

在 .xml 文件中,当您声明片段时,您需要支持 Map Fragment,因为您的 min skd 为 8:

<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:maps="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
于 2013-09-19T05:24:09.507 回答
0

如果您想自动缩放到谷歌地图 v2 中的某个点,您可以这样做

private float previousZoomLevel = 13.00f;
LatLng zoomPoint = new LatLng(12.977286, 77.632720);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(zoomPoint,previousZoomLevel));

这里 previousZoomLevel 是缩放级别

于 2013-09-19T04:43:00.500 回答