0

I want to make an android application for bicycle stations in Tehran. For that I need to show specific and static locations on the map like this:

google map locations example

I tried several libraries from Github but didn't work. I also added an extra location (double latitude1 = 35.747394; double longitude1 = 51.267577;) but the location mark doesn't appear on the map.

I'd appreciate if anyone could help me.

This is my code:

MainActivity.java

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import com.google.android.gms.maps.MapFragment;
import com.playpersia.bicyclemap.R;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;



public class MapsActivity extends FragmentActivity {

    double latitude1 = 35.747394;
    double longitude1 = 51.267577;
    // Google Map
    private GoogleMap googleMap;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        Uri gmmIntentUri = Uri.parse("geo:35.694677, 51.394871");
        Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
        mapIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //***Change Here***
        mapIntent.setPackage("com.google.android.apps.maps");

        if (mapIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(mapIntent);
            finish();

        }

        try {

            initilizeMap();


            MarkerOptions markerOptions1 = new MarkerOptions().position(
                new LatLng(latitude1, longitude1)).title("بوستان جوانمردان");

            googleMap.addMarker(markerOptions1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @
    Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

    private void initilizeMap() {

        if (googleMap == null) {
            SupportMapFragment mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                R.id.map));

            if (googleMap != null) {
                Toast.makeText(getApplicationContext(), "خطا", Toast.LENGTH_LONG).show();
            }


        }

    }

}
abielita
  • 13,147
  • 2
  • 17
  • 59
Hadis
  • 535
  • 3
  • 11
  • 31
  • what is is exactly the problem? you can't add several marker? or the map is not showing ? , looking at your code i see that you are adding just one marker – younes zeboudj Jul 23 '16 at 17:40
  • @youzking yeah I just added one location but it doesn't show that one location – Hadis Jul 24 '16 at 16:17
  • adding a marker is note suffisant you have to move the camera of the map to the added marker, your marker ma be added but it is just not visible, try adding this after adding the marker : `LatLng latLng = new LatLng(latitude1, longitude1); CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10); map.animateCamera(cameraUpdate);` – younes zeboudj Jul 24 '16 at 19:50
  • it didn't work@youzking – Hadis Jul 26 '16 at 10:13

2 Answers2

1

Check the Google Places API documentation. It allows you to query for place information on a variety of categories, such as: establishments, prominent points of interest, geographic locations, and more. You can search for places either by proximity or a text string.

Use Nearby Search which lets you search for places within a specified area. You can refine your search request by supplying keywords or specifying the type of place you are searching for. Here is the list of supported types.

Sample HTTP URL of the following form:

https://maps.googleapis.com/maps/api/place/nearbysearch/output?parameters

where output may be either of the following values:

  • json (recommended) indicates output in JavaScript Object Notation (JSON)
  • xml indicates output as XML

The following example is a search request for places of type 'transit_station' within a 500m radius of a point in Sydney, Australia:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=transit_station&key=YOUR_API_KEY

Check these related links and tutorial:

Hope this helps!

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59
0

I hope you are trying to show map in your app itself.You would have found the mistake if you read your log cat. googleMap variable isn't initialised. So if add marker it will throw null pointer exception. you should use getMapAsync callback to initialise googleMap variable then you can add markers.This will work if your map shows and adding Marker isn't working. If map is not showing the problem with api key. correct that and and remove intent code and try this.

  • I corrected the api key, now it gives me this error: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference 07-24 21:40:04.931 19095-19095/com.playpersia.bicyclemap – Hadis Jul 24 '16 at 17:37